I have a string in variable a
as below:
a = 'foo(123456) together with foo(2468)'
I would like to use "re" to extract both foo(123456)
and foo(2468)
from the string.
I have two questions:
foo(.\*)
doesn't seem to work, as it treats 123456)
together with foo(2468
as .*
Use re.search() to extract a substring matching a regular expression pattern. Specify the regular expression pattern as the first parameter and the target string as the second parameter. \d matches a digit character, and + matches one or more repetitions of the preceding pattern.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
Use re. finditer() to find patterns in a text file compile(pattern) to return a regular expression object. Use the for-loop syntax for item in iterable with iterable as an opened file using open(file) with the file name as file to loop over each line of the file.
A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern.
import re
pattern = re.compile(r'foo\(.*?\)')
test_str = 'foo(123456) together with foo(2468)'
for match in re.findall(pattern, test_str):
print(match)
Two things:
.*?
is the lazy quantifier. It behaves the same as the greedy quantifier (.*
), except it tries to match the least amount of characters possible going from left-to-right across the string. Note that if you want to match at least one character between the parentheses, you'll want to use .+?
.
Use \(
and \)
instead of (
and )
because parentheses are normally used inside regular expressions to indicate capture groups, so if you want to match parentheses literally, you have to use the escape character before them, which is backslash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With