1

ひどいタイトルで申し訳ありません-どのように言えばよいかわかりません。

注:Javascriptでこれをやろうとしています

長さと構造が異なる 2 つの異なる URL 内の特定のテキストを一致させたいと考えています。1 つは say で始まり、もう 1 つはvideosay で始まりますaudio。一致は、文字列の開始に応じて異なる方法で置き換える必要があります。

dotall (.*) を肯定的な後読みで一致させる方法がわかりません。

たとえば、以下の文字列は 2 つの個別の RegEx によって一致する必要があります。1 つはそれを指定し、もう 1 つは指定されvideoた文字列の先頭にのみ一致する 必要があります。audio%7bmatch%7d%7bmatch%7d

video://lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length

audio://lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length

私は試してみました: /(?<=video)(?<=.*)%7bmatch%7d/g http://regexr.com?373gh%7bmatch%7d他の亜種の中で、例のようにすべてを取得せずに、または他の一致 のみに一致するようには見えませんvideo。私は困惑しました。正しい方向に微調整していただければ幸いです。

編集: Javascript に固有の回答:

// Random collection of string(s)
var inputString = "video://lots0fr@ndomcharacasdf/asd/ft#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharact#Rsinbet/asdfas/dweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthvideo://lots0fr@ndomcharact#Rsinbetweasdf/asd/f/asdfenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharaasdfafsdct#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthvideo://lots0fr@ndomcharact#Rsinfasdfasdfbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharaasdfasdf///asdfasdfct#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length";



// Regex to match after every `video` occurence
regexVideo = new RegExp(/(video.*?)%7bmatch%7d/g);

// Regex to match after every `audio` occurence
regexAudio = new RegExp(/(audio.*?)%7bmatch%7d/g);

// strings to replace the matches
var replaceStringVideo = "<span class='woot'>W00tW00tVideoW00t</span>";
var replaceStringAudio = "<span class='woot'>W00tW00tVAudioW00t</span>";

// replace both audio and video with respective replace values NOTE: the dollar token needed concatenated before the replaceString.
inputString.replace(regexVideo, "$1" + replaceStringVideo).replace(regexAudio, "$1" + replaceStringAudio);

// This yields an inputString value of 
"video://lots0fr@ndomcharacasdf/asd/ft#Rsinbetweenof1nd1scrimin@length/<span class='woot'>W00tW00tVideoW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharact#Rsinbet/asdfas/dweenof1nd1scrimin@length/<span class='woot'>W00tW00tVAudioW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthvideo://lots0fr@ndomcharact#Rsinbetweasdf/asd/f/asdfenof1nd1scrimin@length/<span class='woot'>W00tW00tVideoW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharaasdfafsdct#Rsinbetweenof1nd1scrimin@length/<span class='woot'>W00tW00tVAudioW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthvideo://lots0fr@ndomcharact#Rsinfasdfasdfbetweenof1nd1scrimin@length/<span class='woot'>W00tW00tVideoW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@lengthaudio://lots0fr@ndomcharaasdfasdf///asdfasdfct#Rsinbetweenof1nd1scrimin@length/<span class='woot'>W00tW00tVAudioW00t</span>/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length"
4

3 に答える 3

2

PHP または Perl を使用している場合は、\Kアンカーを使用して、次のように正規表現で一致するものをすべて無視できます。

video.*\K%7bmatch%7

残念ながら、JavaScript では、 video から までのすべてに一致する次の正規表現を使用するのが最も簡単な方法%7bmatch%7dです。その後、replace()不要なデータを取り除くために使用できます。

var input = "video://lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length/%7bmatch%7d/lots0fr@ndomcharact#Rsinbetweenof1nd1scrimin@length";
var finalResult = /(?=video.*%7bmatch%7d).*%7bmatch%7d/.exec(input)[0].replace(/.*(?=%7bmatch%7d)/, "");
console.log(finalResult);

正規表現101のデモ

于 2013-11-07T13:24:34.960 に答える
1

そのウェブサイトのバグのようです。を使用する必要があります/(?<=video.*)%7b.*?%7d/gが、その Web サイトは後読みではなく先読みだと考えているようです (いずれかの括弧にカーソルを合わせると確認できます)。


それを置き換えるために一致させる必要がある場合は、次%7b.*?%7dのように、それまでのすべてを一致させ、置換でそれを参照してください。

s/^(video.*?)%7b.*?%7d/$1your_replacement_here/g

regexr.com のデモとregex101.comの別のデモを参照してください。

于 2013-11-07T13:28:46.230 に答える