Joohun Lee
Joohun Lee

Reputation: 187

capturing only certain characters in regular expression by python

I'm trying to create pattern that detects certain strings. Basically, the purpose is if detect 'ABCDEF' in a string then return "true". but between 'ABC' and 'DEF', there would be (L+count) or (number) or not like examples below. BTW all examples below return "true". It would be really appreciate if some one can advise how to ignore those combo of number and characters between "ABC" and "DEF"?

 ABC(L30)(345)DEF, ABC(L2)(45)DEF, ABCDEF, ABC(L10)DEF, ABC(2)DEF

Upvotes: 0

Views: 65

Answers (2)

LetzerWille
LetzerWille

Reputation: 5658

def is_in(string_,word_):

    import re

    return word_ in [''.join(re.split('\(.+?\)\(.+?\)|\(.+?\)',string_))][0]


s = "ABC(L30)(345)DEF, ABC(L2)(45)DEF, ABCDEF, ABC(L10)DEF, ABC(2)DEF"

print(is_in(s,'ABCDEF'))

True

Upvotes: 0

Ken Cheung
Ken Cheung

Reputation: 1808

Regex compose thinking path:

  • Start with ABC
  • Ends with DEF
  • Either (L[0-9]+) or ([0-9]+) or empty
#!/usr/bin/python
import re
r=re.compile("ABC(\(L[0-9]+\)|\([0-9]+\)|)*DEF");
lines = [
        "ABC(L30)(345)DEF",
        "ABC(L2)(45)DEF",
        "ABC(L30)DEF",
        "ABC(345)DEF",
        "ABCDEF",
        "ABCxyzDEF",
        "ABC(L)DEF",
        "ABC(A)DEF",
        "ABC()DEF",
]
for str in lines:
        if r.match(str):
                print "    match : %s" % str
        else:
                print "not match : %s" % str

Output:

    match : ABC(L30)(345)DEF
    match : ABC(L2)(45)DEF
    match : ABC(L30)DEF
    match : ABC(345)DEF
    match : ABCDEF
not match : ABCxyzDEF
not match : ABC(L)DEF
not match : ABC(A)DEF
not match : ABC()DEF

Upvotes: 1

Related Questions