regex-lookarounds

lookahead and non-capturing regular expressions

流过昼夜 提交于 2019-12-19 09:58:23
问题 I'm trying to match the local part of an email address before the @ character with: LOCAL_RE_NOTQUOTED = """ (( \w # alphanumeric and _ | [!#$%&'*+-/=?^_`{|}~] # special chars, but no dot at beginning ) ( \w # alphanumeric and _ | [!#$%&'*+-/=?^_`{|}~] # special characters | ([.](?![.])) # negative lookahead to avoid pairs of dots. )*) (?<!\.)(?:@) # no end with dot before @ """ Testing with: re.match(LOCAL_RE_NOTQUOTED, "a.a..a@", re.VERBOSE).group() gives: 'a.a..a@' Why is the @ printed in

lookahead and non-capturing regular expressions

♀尐吖头ヾ 提交于 2019-12-19 09:58:06
问题 I'm trying to match the local part of an email address before the @ character with: LOCAL_RE_NOTQUOTED = """ (( \w # alphanumeric and _ | [!#$%&'*+-/=?^_`{|}~] # special chars, but no dot at beginning ) ( \w # alphanumeric and _ | [!#$%&'*+-/=?^_`{|}~] # special characters | ([.](?![.])) # negative lookahead to avoid pairs of dots. )*) (?<!\.)(?:@) # no end with dot before @ """ Testing with: re.match(LOCAL_RE_NOTQUOTED, "a.a..a@", re.VERBOSE).group() gives: 'a.a..a@' Why is the @ printed in

Regular expression using negative lookbehind not working in Notepad++

五迷三道 提交于 2019-12-18 18:55:13
问题 I have a source file with literally hundreds of occurrences of strings flecha.jpg and flecha1.jpg , but I need to find occurrences of any other .jpg image (i.e. casa.jpg , moto.jpg , whatever) I have tried using a regular expression with negative lookbehind, like this: (?<!flecha|flecha1).jpg but it doesn't work! Notepad++ simply says that it is an invalid regular expression. I have tried the regex elsewhere and it works, here is an example so I guess it is a problem with NPP's handling of

Java-8 regex negative lookbehind with `\R`

♀尐吖头ヾ 提交于 2019-12-18 13:29:54
问题 While answering another question, I wrote a regex to match all whitespace up to and including at most one newline. I did this using negative lookbehind for the \R linebreak matcher: ((?<!\R)\s)* Afterwards I was thinking about it and I said, oh no what if there is a \r\n ? Surely it will grab the first linebreakish character \r and then I will be stuck with a spurious \n on the front of my next string, right? So I went back to test (and presumably fix) it. However, when I tested the pattern,

Regex - lookahead assertion

試著忘記壹切 提交于 2019-12-18 10:52:44
问题 I have problem with lookahead assertion (?=). For example, I have expression: /Win(?=2000)/ It match Win , if expression is like Win2000 , Win2000fgF . I have next expression: ^(?=.*\d)(?=.*[a-z]).*$ It match for digit and lower case letter, for example: 45dF , 4Dd . But I don't know, why it works and match all characters :) I haven't characters, which are before (?=.*\d) . I think, only this expression should work: ^.\*(?=.*\d)(?=.*[a-z]).*$ (with \* before expression). Could you explain it?

Positive lookahead not working as expected

倖福魔咒の 提交于 2019-12-18 07:08:59
问题 I have the following regex with a positive lookahead: /black(?=hand)[ s]/ I want it to match blackhands or blackhand . However, it doesn't match anything. I am testing on Regex101. What am I doing wrong? 回答1: Lookahead does not consume the string being searched. That means that the [ s] is trying to match a space or s immediately following black . However, your lookahead says that hand must follow black , so the regular expression can never match anything. To match either blackhands or

Is this a bug in .NET's Regex.Split?

扶醉桌前 提交于 2019-12-17 21:04:42
问题 I have two regular expressions, for use with Regex.Split : (?<=\G[^,],[^,],) and (?<=\G([^,],){2}) When splitting the string "A,B,C,D,E,F,G," , the first one results in: A,B, C,D, E,F, G, and the second results in: A,B, A, C,D, C, E,F, E, G, What is going on here? I thought that (X){2} was always equivalent to XX , but I'm not sure anymore. In my actual problem, I need to do something like quite a bit more complex, and I need to do it sixty nine times, so just repeating the pattern is less

Regular expression pattern to match string without any 2 consecutive repeated characters

大城市里の小女人 提交于 2019-12-17 20:28:05
问题 I can very easily write a regular expression to match a string that contains 2 consecutive repeated characters: /(\w)\1/ How do I do the complement of that? I want to match strings that don't have 2 consecutive repeated characters. I've tried variations of the following without success: /(\w)[^\1]/ ;doesn't work as hoped /(?!(\w)\1)/ ;looks ahead, but some portion of the string will match /(\w)(?!\1)/ ;again, some portion of the string will match I don't want any language/platform specific

Understanding negative lookahead

半腔热情 提交于 2019-12-17 16:13:46
问题 I'm trying to understand how negative lookaheads work on simple examples. For instance, consider the following regex: a(?!b)c I thought the negative lookahead matches a position. So, in that case the regex matches any string that contains strictly 3 characters and is not abc . But it's not true, as can be seen in this demo . Why? 回答1: Lookaheads do not consume any characters. It just checks if the lookahead can be matched or not: a(?!b)c So here after matching a it just checks if it is

What's wrong with my lookahead regex in GNU sed?

亡梦爱人 提交于 2019-12-17 09:08:48
问题 This is what I'm doing (simplified example): gsed -i -E 's/^(?!foo)(.*)$/bar\1/' file.txt I'm trying to put bar in front of every line that doesn't start with foo . This is the error: gsed: -e expression #1, char 22: Invalid preceding regular expression What's wrong? 回答1: As far as I know sed has not neither look-ahead nor look-behind. Switch to a more powerful language with similar syntax, like perl . 回答2: sed -i '/^foo/! s/^/bar/' file.txt -i change the file in place /^foo/! only perform