1

I am trying the next code but it seems that i am doing something wrong.

import re

lista = ["\\hola\\01\\02Jan\\05\\03",
"\\hola\\01\\02Dem\\12",
"\\hola\\01\\02March\\12\\04"]

for l in lista:
    m= re.search("\\\\\d{2,2}\\\\\d{2,2}[a-zA-Z]+\\\\\d{2,2}\s",l)
    if m:
        print (m.group(0))

The result should be second string. I have tried without \s but the result match with all strings.

4
  • Can you include expected output in question? Commented Oct 5, 2015 at 20:00
  • yes. i have edited the code because there was an errata. Commented Oct 5, 2015 at 20:03
  • If you just need to get four sequences, you can do this: ^(?:\\\[^\]+){4}$. But if you care about what text those sequences contain, you will need to elaborate. Commented Oct 5, 2015 at 20:10
  • 3
    Tip: use r-strings to avoid escaping many backslashes. Commented Oct 5, 2015 at 20:21

2 Answers 2

2

You can try this regex:

lista = [r"\hola\01\02Jan\05\03", r"\hola\01\02Dem\12", r"\hola\01\02March\12\04"]

>>> for l in lista:
...     m = re.search(r"\\\d{2,2}\\\d{2,2}[a-zA-Z]+\\\d{2}$", l)
...     if m:
...             print m.group()
...

Output:

\01\02Dem\12
  • Use r"..." form to declare a regex and input as raw string
  • Use anchor $ to avoid matching unwanted input
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. with r i had to use only two backslash (`\`) in regex. please correct the code if it possible. thanks once again i have learned new thing about regex.
0

You can use the following code without regex:

>>> for l in lista:
        totalNo = l.count('\\')
        if totalNo == 4:
            print l

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.