Home > Mobile >  How to add or into expression in Python Regex Positive Look Behind
How to add or into expression in Python Regex Positive Look Behind

Time:04-07

I have a working regex in python to look for words after "KEY:". "KEY:" could begin with space or the beginning of a line.

Here is what I have.

((?<= KEY:)|(?<=^KEY:))\w 

Demo Do you have any idea I can simplify this in python?

CodePudding user response:

re.findall(r'(?:(?:^| )KEY:)(\w )', 'KEY:poo, KEY:moo')

extracts the matches by using non-capturing groups to implement what amounts to the equivalent of (what I guess you wanted to look for in) your lookbehind, and capturing parentheses around \w to force findall to return those matches.

For what it's worth, your attempted code doesn't actually work in Python (in any way where it would be useful that I could discover). The regex variant supported by https://regex101.com/ is not exactly the same as the Python re dialect.

However, this works:

re.findall(r'(?:(?<=\sKEY:)|(?<=^KEY:))\w ', 'KEY:poo, KEY:moo')

again, by using non-capturing parentheses instead of capturing ones.

Lookbehinds have to be fixed width with Python's re library so the obvious refactoring (?<=(?:^| )KEY:)\w is not acceptable. The third-party regex library allows variable-width lookahead assertions, if using a lookbehind is important for some reason.

CodePudding user response:

If I understood your problem correctly, you want to match any line that starts with KEY with optional blank characters before it, and you want to return whatever is after KEY: on those lines.

If that's the case, here's the regex I'd use: r"^\s*KEY:(.*)$"

Here is a chunk of code to test it:

import re
find_value_pattern = re.compile(r"^\s*KEY:(.*)$")
examples = [
    "KEY:VALUE",
    " KEY:VALUE",
    "aaaKEY:VALUE",
]
for example in examples:
    match = find_value_pattern.match(example)
    if match is None:
        print(f"'{example}' doesn't match the pattern")
    else:
        print(f"found value '{match.group(1)}' in '{example}'")

and here is the result I get:

found value 'VALUE' in 'KEY:VALUE'
found value 'VALUE' in ' KEY:VALUE'
'aaaKEY:VALUE' doesn't match the pattern
  • Related