Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Extract pattern from string using RegEx

Tags:

python

regex

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:

  1. What is the correct regex to be used? foo(.\*) doesn't seem to work, as it treats 123456) together with foo(2468 as .*
  2. How to extract both foo?
like image 929
user3431399 Avatar asked Apr 02 '15 01:04

user3431399


People also ask

How do you extract a substring from a string in Python regex?

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.

How do I match a pattern in regex?

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).

How do I search for a specific pattern in a file in Python?

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.

What is pattern in regex Python?

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.


1 Answers

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:

  1. .*? 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 .+?.

  2. 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.

like image 151
Shashank Avatar answered Oct 06 '22 02:10

Shashank