Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Javascript "match" in python

I have a method for extracting all the "words" from a string in javascript:

mystring.toLowerCase().match(/[a-z]+/g);

I'd like to convert that same logic (create an array of "words" from my string), but in python. How can I achieve that?

like image 830
Stephane Maarek Avatar asked Dec 10 '14 00:12

Stephane Maarek


People also ask

Is there a match function in Python?

match() function of re in Python will search the regular expression pattern and return the first occurrence. The Python RegEx Match method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object.

Is RegEx in Python the same with Javascript?

They are different; One difference is Python supports Unicode and Javascript doesn't. Read Mastering Regular Expressions. It gives information on how to identify the back-end engines (DFA vs NFA vs Hybrid) that a regex flavour uses. It gives tons of information on the different regex flavours out there.

What is opposite of match in Javascript?

match() returns null in the case of no matches, this works as well: var hasNoMatch = ! foo.

What is match method in Javascript?

match() is an inbuilt function in JavaScript used to search a string for a match against any regular expression. If the match is found, then this will return the match as an array. Syntax: string.match(regExp)


1 Answers

Use findall(), which is similar to String.prototype.match().

import re
regex = r"[a-z]+"
matches = re.findall(regex, strToScan)
like image 154
alex Avatar answered Oct 12 '22 23:10

alex