Just a simple question. I learned that . is a wildcard in regex, and that * means 0 or more. So something like ^hello.* means anything that starts with hello. However, wouldn't ^hello alone achieve the same effect?
Asked
Active
Viewed 158 times
1 Answers
0
The use depends on if the engine is expecting to match the whole string and what you want to do with the result (ie. is a boolean matched enough or do you want the complete or components of the match?)
Many regex will match the same form, but often a more complex regex is wanted for some feature (such as a capture group)
>>> re.match(r"^a", "abc") # string matches, but only a is kept
<re.Match object; span=(0, 1), match='a'>
>>> re.match(r"^a.*", "abc") # full match
<re.Match object; span=(0, 3), match='abc'>
>>> re.match(r"^a(.*)", "abc").group(1) # capture group
'bc'
>>> re.match(r"^a.*d", "abc") # doesn't match trailing d
>>> re.match(r"^a.*d", "abcd") # now jumps center
<re.Match object; span=(0, 4), match='abcd'>
Examples using Python's re
library

ti7
- 16,375
- 6
- 40
- 68