regex-group

Regex match everything between two {}

橙三吉。 提交于 2019-12-05 17:02:20
I was looking at different answers here but unfortunately none of them was good for my case. So I hope you don't mind about it. So I need to match everything between two curly brackets {} except situation when match starts with @ and without these curly brackets e.g: "This is a super text { match_this }" "{ match_this }" "This is another example @{deal_with_it}" Here are my test strings, 1,2,3 are valid while the last one shouldn't be: 1 {eww} 2 r23r23{fetwe} 3 #{d2dded} 4 @{d2dded} I was trying with: (?<=[^@]\{)[^\}]*(?=\}) Then only 2th and 3th options were matches (without the first one)

How can I tell which of the alternatives matched in a Perl regular expression pattern?

烂漫一生 提交于 2019-12-05 02:59:25
问题 I have a list of regular expressions (about 10 - 15) that I needed to match against some text. Matching them one by one in a loop is too slow. But instead of writing up my own state machine to match all the regexes at once, I am trying to | the individual regexes and let perl do the work. The problem is that how do I know which of the alternatives matched? This question addresses the case where there are no capturing groups inside each individual regex. (which portion is matched by regex?)

Regex Non-Duplicate Bigrams

浪尽此生 提交于 2019-12-04 21:14:01
I want a PCRE regex to create bigram pairings similar to this question , but without duplicates words. Full Match: apple orange plum Group 1: apple orange Group 2: orange plum The closest I’ve gotten to it is this, but ‘orange’ isn’t captured in the second group. (\b.+\b)(\g<1>)\b You're looking for this: /(?=(\b\w+\s+\w+))/g Here's a quick perl one-liner to demonstrate it: $ perl -e 'while ("apple orange plum" =~ /(?=(\b\w+\s+\w+))/g) { print "$1\n" }' apple orange orange plum This uses a zero-width lookahead (?=…) around the capture group to ensure we can read the word "orange" twice. If we

PHP How to set the preg-groups to “non-capture” (?:…)

折月煮酒 提交于 2019-12-04 20:24:26
In HTML page, I remove HTML comments like this $contentHTML = preg_replace("#(?=<!--)([\s\S]*?)-->#", "", $contentHTML); But on a huge page for preg_replace , I got " PHP Fatal error: Allowed memory size ... " Perhaps, one solution, would use the non-matching group to avoid capturing text? Could someone explain how use on-matching group ?: Or how can I suppress HTML comments in huge page without preg_replace ? Just unroll the regex as $contentHTML = preg_replace("#<!--[^-]*(?:-(?!->)[^-]*)*-->#", "", $contentHTML); See the regex demo . Comapre with yours taking about 3 times as more steps as

Get the index of the group that matched in a regexp?

喜欢而已 提交于 2019-12-04 16:17:06
I have a regexp: /(alpha)|(beta)|(gamma)/gi Some text to match against: Betamax. Digamma. Alphabet. Hebetation. The matches are: beta, gamma, alpha, beta The values I am looking would be: 1,2,0,1 ...can I ascertain the index of the group that matched in the regexp? Bergi To access the groups , you will need to use .exec() repeatedly: var regex = /(alpha)|(beta)|(gamma)/gi, str = "Betamax. Digamma. Alphabet. Hebetation."; for (var nums = [], match; match = regex.exec(str); ) nums.push(match.lastIndexOf(match[0])); If you want the indizes zero-based, you could use nums.push(match.slice(1)

How can I tell which of the alternatives matched in a Perl regular expression pattern?

余生长醉 提交于 2019-12-03 17:17:10
I have a list of regular expressions (about 10 - 15) that I needed to match against some text. Matching them one by one in a loop is too slow. But instead of writing up my own state machine to match all the regexes at once, I am trying to | the individual regexes and let perl do the work. The problem is that how do I know which of the alternatives matched? This question addresses the case where there are no capturing groups inside each individual regex. ( which portion is matched by regex? ) What if there are capturing groups inside each regexes? So with the following, /^(A(\d+))|(B(\d+))|(C(

How do I group regular expressions past the 9th backreference?

假如想象 提交于 2019-12-03 08:14:28
问题 Ok so I am trying to group past the 9th backreference in notepad++. The wiki says that I can use group naming to go past the 9th reference. However, I can't seem to get the syntax right to do the match. I am starting off with just two groups to make it simple. Sample Data 1000,1000 Regex. (?'a'[0-9]*),([0-9]*) According to the docs I need to do the following. (?<some name>...), (?'some name'...),(?(some name)...) Names this group some name. However, the result is that it can't find my text.

How do I group regular expressions past the 9th backreference?

孤街浪徒 提交于 2019-12-02 21:57:39
Ok so I am trying to group past the 9th backreference in notepad++. The wiki says that I can use group naming to go past the 9th reference. However, I can't seem to get the syntax right to do the match. I am starting off with just two groups to make it simple. Sample Data 1000,1000 Regex. (?'a'[0-9]*),([0-9]*) According to the docs I need to do the following. (?<some name>...), (?'some name'...),(?(some name)...) Names this group some name. However, the result is that it can't find my text. Any suggestions? You can simply reference groups > 9 in the same way as those < 10 i.e $10 is the tenth

Converting a Regex Expression that works in Chrome to work in Firefox [duplicate]

和自甴很熟 提交于 2019-12-02 12:56:58
This question already has an answer here: Javascript: negative lookbehind equivalent? 13 answers I have this Regex Expression that works in chrome but doesn't not work in Firefox. SyntaxError: invalid regexp group It has something to do with lookbehinds and Firefox does not support these. I need this to work in Firefox can some one help me convert this so it works in Firefox and filters out the tags as well? return new RegExp(`(?!<|>|/|&amp|_)(?<!</?[^>]*|&[^;]*)(${term})`, 'gi'); }; searchTermsInArray.forEach(term => { if (term.length) { const regexp = this.regexpFormula(term); newQuestion

How to loop through, match and replace?

六眼飞鱼酱① 提交于 2019-12-02 03:12:04
I have multiple strings with same curly braces I want to replace them as dynamic if I get the count as 1 then need to replace the first occurrence, If count as 2 then replaces the second occurrence as so on until condition satisfies. <?php include_once("con.php"); $db = new Da(); $con = $db->con(); $String = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}"; $Count = 1; if(preg_match_all("/\{\{[^{}]+\}\}/", $lclString, $matches)) { foreach ($matches[0] as $match) { $Count++; $Query = "SELECT link FROM student WHERE linkVal = '".$match."'"; $Result = $con->query($Query); if($row = $Result-