0

I need to find whether a given string is in this format or not.

anystr3ing1 : somesrritn3g

following is my code

prog = re.compile("([a-zA-Z0-9]\D)" + ":" + ([a-zA-Z0-9]\D)")

with open('data.txt','ru') as openfileobject:
  for line in openfileobject:
    if prog.match(line):
      print line

however its not giving any output

2
  • 2
    You have a syntax error. Is this the case in your actual code? Look at the first line, you are missing a " between + and ([a-zA-Z0-9]\D)") near the end of the line. Commented Oct 7, 2015 at 11:49
  • Also, this site will help to quickly test out regex: pythex.org Commented Oct 7, 2015 at 11:52

1 Answer 1

3

Change the regex to:

[a-zA-Z0-9]+\s+:\s+[a-zA-Z0-9]+

Your problem is that you only matched one character from the class [a-zA-Z0-9], followed by a non digit character, then ":" followed by [a-zA-Z0-9].

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.