Found 10784 Articles for Python

How to use range in Python regular expression?

Rajendra Dharmkar
Updated on 12-Jun-2020 07:38:30

2K+ Views

Range in Regular ExpressionsRanges of characters can be indicated by giving two characters and separating them by a '-', for example [a-z] will match any lowercase ASCII letter, [0-5][0-9] will match all the two-digits numbers from 00 to 59.If - is escaped (e.g. [a\-z]) or if it’s placed as the first or last character (e.g. [a-]), it will match a literal '-'. The regex [A-Z] matches all uppercase letters from A to Z. Similarly the regex [a-c] matches the  lowercase letters from a to z.The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's ... Read More

How to write Python regular expression to get all the anchor tags in a webpage?

Rajendra Dharmkar
Updated on 20-Feb-2020 07:56:17

151 Views

The following code extracts all tags in the given stringExampleimport re rex = re.compile(r'[\]') l = "this is text1 hi this is text2" print rex.findall(l)Output['', '']

How to use wildcard in Python regular expression?

Rajendra Dharmkar
Updated on 20-Feb-2020 07:49:42

2K+ Views

The following code uses the Python regex .()dot character for wildcard which stands for any character other than newline.Exampleimport re rex = re.compile('th.s') l = "this, thus, just, then" print rex.findall(l)OutputThis gives the output['this', 'thus']

How to use special characters in Python Regular Expression?

Rajendra Dharmkar
Updated on 13-Jun-2020 07:12:45

6K+ Views

From Python documentationNon-special characters match themselves. Special characters don't match themselves −\ Escape special char or start a sequence..Match any char except newline, see re.DOTALL^Match start of the string, see re.MULTILINE $  Match end of the string, see re.MULTILINE[ ]Enclose a set of matchable charsR|S Match either regex R or regex S.()Create capture group, & indicate precedenceAfter '[', enclose a set, the only special chars are −]End the set, if not the 1st char-A range, eg. a-c matches a, b or c^Negate the set only if it is the 1st char    Quantifiers (append '?' for non-greedy) −{m}Exactly m repetitions {m, n}From m (default 0) ... Read More

How to write Python Regular Expression find repeating digits in a number?

Rajendra Dharmkar
Updated on 20-Feb-2020 05:55:55

540 Views

The following code using Python regex to find the repeating digits in given stringExampleimport re result = re.search(r'(\d)\1{3}','54222267890' ) print result.group()OutputThis gives the output2222

How to find all adverbs and their positions in a text using python regular expression?

Rajendra Dharmkar
Updated on 20-Feb-2020 07:40:37

334 Views

As per Python documentationIf one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. If one was a writer who wanted to find all of the adverbs and their positions in some text, he or she would use finditer() in the following manner −>>> text = "He was carefully disguised but captured quickly by police." >>> for m in re.finditer(r"\w+ly", text): ...     print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0))) 07-16: carefully 40-47: quickly

How can I find all matches to a regular expression in Python?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

103 Views

We use re.findall or re.finditer methods to find all matches to a regular method.re.findall(pattern, string) returns a list of matching strings.re.finditer(pattern, string) returns an iterator over MatchObject object

How to get the number of capture groups in Python regular expression?

Rajendra Dharmkar
Updated on 20-Feb-2020 07:57:19

1K+ Views

The following code gets the number of captured groups using Python regex in given stringExampleimport re m = re.match(r"(\d)(\d)(\d)", "632") print len(m.groups())OutputThis gives the output3

How to capture an exception raised by a Python Regular expression?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

878 Views

When the match method is implemented, if it turns out that there are no matches, then None is returned. There are no functions in the re module that throw up an exception when the list or matches is emptyexception re.errorException raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern.

How do I clear the regular expression cache in Python?

Rajendra Dharmkar
Updated on 13-Jun-2020 06:52:00

529 Views

Presently, when regular expressions are compiled, the result is cached so that if the same regex is compiled again, it is retrieved from the cache and no extra effort is required. This cache supports up to 100 entries. Once the 100th entry is reached, the cache is cleared and a new compile must occur.The objective of caching is to decrease the average call time of the function. The overhead associated with keeping more information in _cache and paring it instead of clearing it would increase that average call time. The _cache.clear() call will complete quickly, and even though cache is ... Read More

Advertisements