0

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

3
  • Show your attempt please. Commented Sep 16, 2013 at 21:05
  • 1
    Is the sentence really like that, with "in" instead of "is"? (It's important to be precise here, because a regexp isn't going to figure out what you really meant and let it slide.) Commented Sep 16, 2013 at 21:06
  • 1
    If you really don't know basic regexp stuff like this, you should either (a) work through a tutorial (at least the Regular Expression HOWTO in the docs, but maybe something more friendly you can find online), or (b) not use regexps until you're willing to do so. Otherwise, you're just copying and pasting magic code without understanding what it does, which makes it impossible to debug or improve your code. Commented Sep 16, 2013 at 21:11

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.