0

I have a string that have this format:

some text <br>septembar 1989<br>

And I'm using this regex find the month and year part

<br/?>(?!=b\.)(.*?\b\d{4}\b)

and I get what i want-septembar 1989

However, I now have situation when <br> is inserted

<br>some text <br>septembar 1989<br>

result: some text <br>septembar 1989

Can you suggest how to modify my existing pattern to support both cases? I guess I need somehow to exlude <br> from matching in .*?

1
  • Your pattern doesn't work Commented Mar 4, 2013 at 13:20

3 Answers 3

0

Try this

<br/?>([^<]+)\d{4}

[^<] means match anything except an opening tag which is what you want.

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

Comments

0

I written a simple code, may be you find helpful unto some extend:

import re
def getDate(str):
 m = re.match("[\<br>]*[\w\s]*\<br>([\w\s]*[12][0-9]{3})",str);
 return m.group(1)

print getDate("some text <br>dec 1989<br>");
print getDate("<br> some text <br>septembar 1989<br>");
print getDate("grijesh chuahan <br>feb 2009<br>");

Output:

dec 1989
septembar 1989
feb 2009

Comments

0
import re

ss = 'dfgqeg<br>some text <br>septembar 1989<br>'

reg = re.compile('<br(?: /)?>'
                 '(?!.+?<br(?: /)?>.+?<br(?: /)?>)'
                 '(.+?\d{4})'
                 '<br(?: /)?>')

print reg.search(ss).group(1)

.

  • '<br(?: /)?>' catches <br> and <br /> occurrences

.

  • '(?!.+?<br(?: /)?>.+?<br(?: /)?>)' is a look-ahead assertion,
    it verifies that after the position where it starts in the analyzed text, there isn't the suite of characters described as a succession of :

    • .+? any kind of characters, but the ? orders that this portion must stop as soon as <br> or <br /> is encountered
    • <br> or <br />
    • again any kind of characters stopping before <br> or <br />
    • <br> or <br />

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.