Reputation: 217
I am new to python and trying to find out how way to match a sentence with variable words
for examples 'The file test.bed in successfully uploaded'
Now here in the above sentence, the file name would change (it could sample.png) and rest of the words would be same.
Can anybody let me know what is the best way using regular expression to match the sentence.
thanks
Upvotes: 0
Views: 215
Reputation: 365657
If you just want to match anything there:
r'The file (.+?) in successfully uploaded'
The .
means any character, and the +
means one or more of the preceding.
The ?
means to do it non-greedily, so if you have two sentences in a row, like "The file foo.bar is successfully uploaded. The file spam.eggs is successfully uploaded."
, it'll match "foo.bar"
, and then "spam.eggs"
, rather than just finding one match "foo.bar is successfully uploaded. The file spam.eggs"
. You may not need it in your application.
Finally, the parentheses are how you mark part of a pattern as a group that you can extract from the match object.
But what if you want to match just valid filenames? Well, you'll need to come up with a rule for valid filenames, which may be different depending on your application. Is it Windows-specific? Is whatever you're parsing quoting filenames with spaces? And so on.
Upvotes: 2