regex-lookarounds

Regex Lookahead and Lookbehinds: followed by this or that

青春壹個敷衍的年華 提交于 2019-12-01 13:31:28
I'm trying to write a regular expression that checks ahead to make sure there is either a white space character OR an opening parentheses after the words I'm searching for. Also, I want it to look back and make sure it is preceded by either a non-Word ( \W ) or nothing at all (i.e. it is the beginning of the statement). So far I have, "(\\W?)(" + words.toString() + ")(\\s | \\()" However, this also matches the stuff at either ends - I want this pattern to match ONLY the word itself - not the stuff around it. I'm using Java flavor Regex. As you tagged your question yourself, you need

Regular Expressions, understanding lookbehind in combination with the or operator

牧云@^-^@ 提交于 2019-12-01 12:07:17
This is more a question of understanding than an actual problem. The situation explains as follows. I got some float numbers (e.g. an amount of money) between two quotation marks "". Examples: "1,23" "12,23" "123,23" Now I wanted to match the comma in those expressions. I built the following regex which works for me: (?<=\"[0-9]|[0-9]{2})(,)(?=[0-9]{2}\") The part which I don't completly understand is the lookbehind in combination with the or "|". But let's break it up: ( ?<= //Start of the lookbehind \" //Starting with an escaped quotation mark " [0-9] //Followed by a digit between 0 and 9

lookahead and non-capturing regular expressions

倾然丶 夕夏残阳落幕 提交于 2019-12-01 09:25:30
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 the output, even though I'm using a non-capturing group (?:@) ? Testing with: re.match(LOCAL_RE

Regex to match certain characters and exclude certain characters but without negative lookahead

爱⌒轻易说出口 提交于 2019-12-01 08:49:44
I want a regex that matches all emojis (or most of them) but excludes certain characters (such as “|”|‘|’|…|— ). This regex does the job via negative lookahead: /(?!\u201C|\u201D|\u2018|\u2019|\u2026|\u2014)(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/ But apparently Google Scripts doesn't support this. Error: Invalid regular expression pattern (?!“|”|‘|’|…|—)(©|®|[ -㌀]|?[퀀-?]|?[퀀-?]|?[퀀-?]) Is there another way to achieve my goal (a regex that works with Google Script's findText )? Option 1 Maybe, [\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u

Match the body of a function using Regex

拜拜、爱过 提交于 2019-12-01 08:39:08
Given a dummy function as such: public function handle() { if (isset($input['data']) { switch($data) { ... } } else { switch($data) { ... } } } My intention is to get the contents of that function, the problem is matching nested patterns of curly braces {...} . I've come across recursive patterns but couldn't get my head around a regex that would match the function's body. I've tried the following (no recursion): $pattern = "/function\shandle\([a-zA-Z0-9_\$\s,]+\)?". // match "function handle(...)" '[\n\s]?[\t\s]*'. // regardless of the indentation preceding the { '{([^{}]*)}/'; // find

Select the next line after match regex

自作多情 提交于 2019-12-01 08:11:56
I'm currently using a scanning software "Drivve Image" to extract certain information from each paper. This software enables certain Regex code to be run if needed. It seems to be run with the UltraEdit Regex Engine. I get the following scanned result: 1. 21Sid1 2. Ordernr 3. E17222 4. By 5. Seller I need to search the string for the text Ordernr and then pick the following line E17222 which in the end will be said filename of the scanned document. I will never know the exact position of these two values in the string. That is why I need to focus on Ordernr because the text I need will always

Select the next line after match regex

倾然丶 夕夏残阳落幕 提交于 2019-12-01 06:42:14
问题 I'm currently using a scanning software "Drivve Image" to extract certain information from each paper. This software enables certain Regex code to be run if needed. It seems to be run with the UltraEdit Regex Engine. I get the following scanned result: 1. 21Sid1 2. Ordernr 3. E17222 4. By 5. Seller I need to search the string for the text Ordernr and then pick the following line E17222 which in the end will be said filename of the scanned document. I will never know the exact position of

Java-8 regex negative lookbehind with `\\R`

别等时光非礼了梦想. 提交于 2019-11-30 09:50:26
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, it matched an entire \r\n . It does not match only the \r leaving \n as one might expect. "\r\n"

Lookahead vs lookbehind

做~自己de王妃 提交于 2019-11-30 09:48:37
I have a hard time to understand the concepts of "lookahead" and "lookbehind". For example, there is a string "aaaaaxbbbbb". If we look at "x", does lookahead mean looking "x" towards "bbbbb" or "aaaaa"? I mean the direction. If the regex is x(?=insert_regex_here) that is a (positive) look*ahead*, which looks ahead , or forwards , in other words towards "bbbb". It means "find an x that is followed by insert_regex_here ". If the regex is (?<=insert_regex_here)x that is a (positive) look*behind*, which looks behind , or backwards , in other words towards "aaaa". It means "find an x that is

Regex matching multiple negative lookahead

我怕爱的太早我们不能终老 提交于 2019-11-30 08:50:53
I'm trying to match a string (using a Perl regex) only if it doesn't start with "abc:" or "defg:", but I can't seem to find out how. I've tried something like ^(?:(?!abc:)|(?!defg:)) Lookahead (?=foo) , (?!foo) and lookbehind (?<=foo) , (?<!foo) do not consume any characters. You can: ^(?!abc:)(?!defg:) or ^(?!defg:)(?!abc:) The order does not make a difference. Try doing this : ^(?!(?:abc|defg):) … or could have dropped the alternation from the original expression: ^(?:(?!abc:)(?!defg:)) AJP ^(?!abc:|defg:)\s*\w+ use this regex. this will avoid line start with "abc:" and "defg:" as you want.