lookbehind

Backreferences in lookbehind

久未见 提交于 2019-11-27 08:43:43
Can you use backreferences in a lookbehind? Let's say I want to split wherever behind me a character is repeated twice. String REGEX1 = "(?<=(.)\\1)"; // DOESN'T WORK! String REGEX2 = "(?<=(?=(.)\\1)..)"; // WORKS! System.out.println(java.util.Arrays.toString( "Bazooka killed the poor aardvark (yummy!)" .split(REGEX2) )); // prints "[Bazoo, ka kill, ed the poo, r aa, rdvark (yumm, y!)]" Using REGEX2 (where the backreference is in a lookahead nested inside a lookbehind) works, but REGEX1 gives this error at run-time: Look-behind group does not have an obvious maximum length near index 8 (?<=(.)

Regular Expression Lookbehind doesn't work with quantifiers ('+' or '*')

本小妞迷上赌 提交于 2019-11-27 07:48:57
I am trying to use lookbehinds in a regular expression and it doesn't seem to work as I expected. So, this is not my real usage, but to simplify I will put an example. Imagine I want to match "example" on a string that says "this is an example". So, according to my understanding of lookbehinds this should work: (?<=this\sis\san\s*?)example What this should do is find "this is an", then space characters and finally match the word "example". Now, it doesn't work and I don't understand why, is it impossible to use '+' or '*' inside lookbehinds? I also tried those two and they work correctly, but

Why doesn't finite repetition in lookbehind work in some flavors?

被刻印的时光 ゝ 提交于 2019-11-27 06:24:45
问题 I want to parse the 2 digits in the middle from a date in dd/mm/yy format but also allowing single digits for day and month. This is what I came up with: (?<=^[\d]{1,2}\/)[\d]{1,2} I want a 1 or 2 digit number [\d]{1,2} with a 1 or 2 digit number and slash ^[\d]{1,2}\/ before it. This doesn't work on many combinations, I have tested 10/10/10 , 11/12/13 , etc... But to my surprise (?<=^\d\d\/)[\d]{1,2} worked. But the [\d]{1,2} should also match if \d\d did, or am I wrong? 回答1: On lookbehind

Does lookaround affect which languages can be matched by regular expressions?

蓝咒 提交于 2019-11-27 06:12:22
There are some features in modern regex engines which allow you to match languages that couldn't be matched without that feature. For example the following regex using back references matches the language of all strings that consist of a word that repeats itself: (.+)\1 . This language is not regular and can't be matched by a regex that does not use back references. Does lookaround also affect which languages can be matched by a regular expression? I.e. are there any languages that can be matched using lookaround that couldn't be matched otherwise? If so, is this true for all flavors of

Java regex error - Look-behind group does not have an obvious maximum length

被刻印的时光 ゝ 提交于 2019-11-26 23:07:39
问题 I get this error: java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 22 ([a-z])(?!.*\1)(?<!\1.+)([a-z])(?!.*\2)(?<!\2.+)(.)(\3)(.)(\5) ^ I'm trying to match COFFEE , but not BOBBEE . I'm using java 1.6. 回答1: Java doesn't support variable length in look behind. In this case, it seems you can easily ignore it (assuming your entire input is one word): ([a-z])(?!.*\1)([a-z])(?!.*\2)(.)(\3)(.)(\5) Both lookbehinds do not add anything: the

Lookbehind on regex for VBA?

时光怂恿深爱的人放手 提交于 2019-11-26 23:06:07
Is there a way to do negative and positive lookbehind in VBA regex? I want to not match if the string starts with "A", so I am currently doing ^A at the start of the pattern, then removing the first character of match(0). Obviously not the best method! I am using the regExp object. brettdj VBA offers positive and negative lookaheads but rather inconsistently not lookbehind. The best example of using Regex with VBA that I have seen is this article by Patrick Matthews [Updated example using Execute rather than Replace ] While I am not completely clear on your usage you could use a function like

Regular Expression Lookbehind doesn't work with quantifiers ('+' or '*')

非 Y 不嫁゛ 提交于 2019-11-26 22:13:25
问题 I am trying to use lookbehinds in a regular expression and it doesn't seem to work as I expected. So, this is not my real usage, but to simplify I will put an example. Imagine I want to match "example" on a string that says "this is an example". So, according to my understanding of lookbehinds this should work: (?<=this\sis\san\s*?)example What this should do is find "this is an", then space characters and finally match the word "example". Now, it doesn't work and I don't understand why, is

What's the technical reason for “lookbehind assertion MUST be fixed length” in regex?

会有一股神秘感。 提交于 2019-11-26 20:23:06
For example,the regex below will cause failure reporting lookbehind assertion is not fixed length : #(?<!(?:(?:src)|(?:href))=["\']?)((?:https?|ftp)://[^\s\'"<>()]+)#S Such kind of restriction doesn't exist for lookahead . Lookahead and lookbehind aren't nearly as similar as their names imply. The lookahead expression works exactly the same as it would if it were a standalone regex, except it's anchored at the current match position and it doesn't consume what it matches. Lookbehind is a whole different story. Starting at the current match position, it steps backward through the text one

Backreferences in lookbehind

大兔子大兔子 提交于 2019-11-26 17:45:56
问题 Can you use backreferences in a lookbehind? Let's say I want to split wherever behind me a character is repeated twice. String REGEX1 = "(?<=(.)\\1)"; // DOESN'T WORK! String REGEX2 = "(?<=(?=(.)\\1)..)"; // WORKS! System.out.println(java.util.Arrays.toString( "Bazooka killed the poor aardvark (yummy!)" .split(REGEX2) )); // prints "[Bazoo, ka kill, ed the poo, r aa, rdvark (yumm, y!)]" Using REGEX2 (where the backreference is in a lookahead nested inside a lookbehind) works, but REGEX1 gives

How does the regular expression ‘(?<=#)[^#]+(?=#)’ work?

江枫思渺然 提交于 2019-11-26 13:22:13
I have the following regex in a C# program, and have difficulties understanding it: (?<=#)[^#]+(?=#) I'll break it down to what I think I understood: (?<=#) a group, matching a hash. what's `?<=`? [^#]+ one or more non-hashes (used to achieve non-greediness) (?=#) another group, matching a hash. what's the `?=`? So the problem I have is the ?<= and ?< part. From reading MSDN, ?<name> is used for naming groups, but in this case the angle bracket is never closed. I couldn't find ?= in the docs, and searching for it is really difficult, because search engines will mostly ignore those special