Reputation: 3
i am testing the re expressions and the book example was:
if re.search('^X\S*: [0-9.]+', line):
But with my expression I reach the same result:
if re.search('^X\S*: [0-9]', line):
What's is the difference?, what do I not see?. Thank you.
Upvotes: 0
Views: 60
Reputation: 821
If you are simply trying to match any line that begins with X: and a number, these are functionally equivalent within the context of your program. They will both find and match lines like these, and return TRUE for your IF statement:
X: 100
X: 9.23
X: 9912.2434.2424.2435
Xabc: 1
X-rf: 0.7
However, the patterns are not the same and if you use them in a different context, they may no longer be functionally equivalent. For example, if you need to match the entire line, you would have to use re.search('^X\S*: [0-9.]+', line)
. The other pattern, re.search('^X\S*: [0-9]', line)
, only will match a single digit 0-9 and nothing else after that.
Upvotes: 1
Reputation: 24168
This is two completely different regex expressions:
^X\S*: [0-9.]+
^X\S*: [0-9]
Comparing side-by-side yields the difference: [0-9.]+
vs [0-9]
.
The second one will only match one digit.
While the first one will match one number of digits and dots.
So, the second one will fail for the following examples:
X: 1.23
X: 123.3213.23131
And any other combination of digits + dot.
The same result will be only for something like this:
X: 1
Upvotes: 1