| related |
|
|---|
Note that re.match() matches from the start of the string. Use re.search() when you want to match anywhere in a string.
- Use
re.search()if you want to search anywhere inside a string - Use re.sub() if you want to substitute substrings.
- Use str.split() if you want to extract fields when you have a common field separator.
import re
result = re.match(pattern, string, flags=0);
Use this if you use a pattern multiple times.
import re
pattern = re.compile('some pattern')
result = pattern.match(string [, pos [, end]]);
result = re.match(r'abc', input) # Check for substring 'abc'
result = re.match(r'^\w+$', input) # Ensure string is one word
pattern = re.compile('abc') # Same as first example
result = pattern.match(input)
And in a condition
if re.match(r'some pattern', input):
print('It matches!'