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?
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.
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.
match() returns null in the case of no matches, this works as well: var hasNoMatch = ! foo.
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)
Use findall()
, which is similar to String.prototype.match()
.
import re
regex = r"[a-z]+"
matches = re.findall(regex, strToScan)
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