0

I've been searching for the answer, but i really cannot find it, thanks for any possible answer!

I'm going through an html document full of random text, and i'm looking for something in a specific date format:

%%/%%/%% or %%-%%-%%

Two questions: 1-is it possible to do something like str.find(%s/%s/%s) while specifying the kind of %s (two digits in this case)?

2-Is it possible to assign the %s to variables while doing the .find? Because something like str.find(%s/%s/%s) % ( d,m,y) gives an error.

Thumbs up for a hint on the python code to achieve this! Thanks!

1 Answer 1

1

Yes, it's very possible. Regular expressions are going to be your best friend in this situation.

For the example you gave, where you're looking for things like "xx/xx/xx" such that each 'x' is a digit, the following regular expression will do the job: \d\d[/]\d\d[/]\d\d.

Here is how it would work in Python:

import re
pattern = re.compile('\d\d[/]\d\d[/]\d\d')
pattern.findall(' sadfsd  04/06/76   kjadsf 10/10/14  ')

Try that out in your Python interpreter and you'll get a list of matching substrings:

['04/06/76', '10/10/14']

If you want to extract the day, month, and year individually, use parentheses to group those parts of the regular expression. Like so:

import re
pattern = re.compile('(\d\d)[/](\d\d)[/](\d\d)')
pattern.findall(' sadfsd  04/06/76   kjadsf 10/10/14  ')

That gets you a list of tuples:

[('04', '06', '76'), ('10', '10', '14')]
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the super fast reply! i guess i'll take a deep look into them. Does it cover even the second question?
I don't understand the second question. Are you trying to get the values out individually? In other words, assign the date substring to d, and so on?
Yea my bad it was poorly presented but you already answered my 2dn question with your more than fast answer. Thank you very much!
I updated my answer with an additional example. If I've answered your question completely, please accept and upvote my answer.
Sadly i've not enough reputation to upvote you yet, but it's a matter of days and i will come back to thank you again!

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.