0

I am having trouble converting a RegEx expression to python. I know that '(\\d+)' is the expression for a single integer, but I cannot figure out how to get an integer that is [2-9].

The RegEx expression is as follows:

[2-9][p-z][a-h][2-9][a-z]*[p-z][2-9][p-z][2-9][p-z]

This is my current expression but it produces many false positives as it is not specific enough:

          re1='(\\d+)'    # Integer Number 1
            re2='([a-z])'   # Any Single Word Character (Not Whitespace) 1
            re3='([a-z])'   # Any Single Word Character (Not Whitespace) 2
            re4='(\\d+)'    # Integer Number 2
            re5='((?:[a-z][a-z]+))' # Word 1
            re6='(\\d+)'    # Integer Number 3
            re7='([a-z])'   # Any Single Word Character (Not Whitespace) 3
            re8='(.)'   # Any Single Character 1
            re9='([a-z])'   # Any Single Word Character (Not Whitespace) 4
            ## Regex search for passcodes ## Thanks to Pierluigi Failla
            rg = re.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9,re.IGNORECASE|re.DOTALL)
            m = rg.search(txt)
            if m:
                int1=m.group(1)
                w1=m.group(2)
                w2=m.group(3)
                int2=m.group(4)
                word1=m.group(5)
                int3=m.group(6)
                w3=m.group(7)
                c1=m.group(8)
                w4=m.group(9)
                txt2='"'+int1+w1+w2+int2+word1+int3+w3+c1+w4+'"'
                return [txt2]
8
  • 5
    But [2-9] is correct for matching an integer that is 2-9. So what do you want to match in the first place? What are false positives you are getting? What are examples of strings you are matching against? Commented May 20, 2013 at 17:08
  • This question is very unclear. The first expr and the code below do not match. Commented May 20, 2013 at 17:09
  • 1
    You want to use r'' raw strings to avoid all the doubled \`, and there is a re.VERBOSE` mode to make writing readable expressions easer than concatenating 9 separate strings.. Commented May 20, 2013 at 17:13
  • 1
    May be worth taking a read of this developers.google.com/edu/python/regular-expressions Commented May 20, 2013 at 17:16
  • 1
    \d+ isn't the pattern for one digit, it is for "one or a succession of digits" Commented May 20, 2013 at 17:18

2 Answers 2

2

You should be able to use the range 2-9 in Python, like so: re1 = re.compile(r'[2-9]'). A test in my console then showed that re1.match('7') returns a MatchObject as you want, whereas re1.match('0') returns None, also as you want.

You also appear to have used the range [a-z] in re2, where you said you wanted [p-z] - similar issues in the other character ranges.

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

1 Comment

Thanks, I was aware that the expression I was using was a lot more lenient that I wanted to be, I was unsure if using [2-9] was correct syntax for python.
1

I propose this code, based on what I see in your question:

import re

pat = ('([2-9])'        # Integer Number 1
       '([p-z])'        # Any Single Word Character (Not Whitespace) 1
       '([a-h])'        # Any Single Word Character (Not Whitespace) 2
       '([2-9])'        # Integer Number 2
       '([a-z]*[p-z]+)' # Word 1
       '([2-9])'        # Integer Number 3
       '([p-z])'        # Any Single Word Character (Not Whitespace) 3
       '(.)'            # Any Single Character 1
       '([p-z])'        # Any Single Word Character (Not Whitespace) 4
       )
rg = re.compile(pat)

txt = 'jiji4pa6fmlgkfmoaz8p#q,,,,,,,,,,'
m = rg.search(txt)
if m:
    text2 = "%s%s%s%s%s%s%s%s%s" % m.groups()
    print text2

# prints 4pa6fmlgkfmoaz8p#q

EDIT

text2 = ''.join(m.groups())  # is better

3 Comments

Based on other comments the \d is still matching every integer. Would't it be more specific to use ([2-9]) instead?
Oh I'm really sorry. I copied from the series of re1,re2,etc but you're right: if you doesn't want digits 0 and 1, you must use [2-9] instead of \d. I corrected my code
@Mondrianaire Thank you. I doesn't know where was my brain today; I edited my answer to give a simpler way to obtain the result in the case of this problem.

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.