Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Python regexes support something like Perl's \G?

Tags:

python

regex

perl

I have a Perl regular expression (shown here, though understanding the whole thing isn't hopefully necessary to answering this question) that contains the \G metacharacter. I'd like to translate it into Python, but Python doesn't appear to support \G. What can I do?

like image 312
mike Avatar asked Feb 09 '09 20:02

mike


People also ask

Is regex different from Python?

Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).

How do I check if a regular expression is valid in Python?

fullmatch(). This method checks if the whole string matches the regular expression pattern or not. If it does then it returns 1, otherwise a 0.

Which of the following modules support regular expressions in Python?

The Python "re" module provides regular expression support.

How does regex work Python?

Regular Expressions, also known as “regex” or “regexp”, are used to match strings of text such as particular characters, words, or patterns of characters. It means that we can match and extract any string pattern from the text with the help of regular expressions.


1 Answers

Try these:

import re
re.sub()
re.findall()
re.finditer()

for example:

# Finds all words of length 3 or 4
s = "the quick brown fox jumped over the lazy dogs."
print re.findall(r'\b\w{3,4}\b', s)

# prints ['the','fox','over','the','lazy','dogs']
like image 53
Kenan Banks Avatar answered Sep 23 '22 01:09

Kenan Banks