1

I have 3 strings

a ="keep the your pass ABCDEFG other text"
b ="your pass: TESTVALUE other text"
c ="no pass required other text"

I want to get capital values after pass, like this

re.match(r'.*\spass:?\s([a-zA-Z]+).*',a,re.I).group(1)
re.match(r'.*\spass:?\s([a-zA-Z]+).*',b,re.I).group(1)

but I want to exclude "no pass", which is I don't want re to match to c string, how do I do that?


Solution: Thanks eyquem and ovgolovin

I will take eyquem's suggestion of re.search('no\s+pass|pass:?\s+([A-Z]+)')

0

3 Answers 3

3
import re

for x in ("keep the your pass ABCDEFG other text",
          "your pass: TESTVALUE other text",
          "no pass required other text"):
    print re.search('no\s+pass|pass:?\s+([A-Z]+)',x).group(1)
A-Z]+)'

result

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

1 Comment

Nice. I was hoping to use a re.findall for something similar, but it doesn't use group(x). Hrm.
1

It's not OK to use match here. It's preferable to use search for such cases.

re.search(r'(?<!no\s)pass:?\s+([A-Z]+)',a).group(1)

It would be better to write it this way:

re.search(r'(?<!no\s*)pass:?\s+([A-Z]+)',a).group(1)

, but unfortunatelly the current version of regex engine doesn't support infinite lookbehinds.

1 Comment

@user976557 Please, read why it's preferable to use search, not match (I provided the link).
1

A solution would be to first , filter everything which doesn't contains 'no pass' and then search for pass. Doing two steps might seem a bit heavy but you will avoid lots of problems by doing it this way. You are trying to solve two problems at the same time (and apparently you are struggling to do it) so just separate the two problems.

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.