0

I have this bit of code:

import re
word = 'baz'
regexp = re.compile(r'a[s|z|d]')
if regexp.search(word) is not None:
    print 'matched'
else:
    print 'not matched'

Which I got from SO. That works and prints matched. Now I am trying to get the same thing to work for a different regex which I've got working in PHP.

This /[a-zA-Z0-9_.-]+$ regex works to filter out these results

mixed_CASE_word_00008908908808908080 # correctly matches
word_with_characters_I_dont_want-(1) # correctly does not match

I want to change the above python code to do the same but I'm not familiar with python and I'm struggling. I've so far got:

import re
word = 'mixed_CASE_word_00008908908808908080'
regexp = re.compile(r'/[a-zA-Z0-9_.-]+$')
if regexp.search(word) is not None:
    print 'matched'
else:
    print 'not matched'

But this gives me the following result:

mixed_CASE_word_00008908908808908080 # not matched
word_with_characters_I_dont_want-(1) # not matched

And I want the code to produce this result

mixed_CASE_word_00008908908808908080 // matched
word_with_characters_I_dont_want-(1) // not matched

Any ideas where I am going wrong?

4
  • 1
    You already have 2 answers, but anyways, this is the tool I always use to check my regex: regex101.com (they should actually call it regex911, because by the time I use it I'm already freaking out, lol) Commented Jan 24, 2015 at 15:21
  • 1
    a[s|z|d] matches a| also. Commented Jan 24, 2015 at 15:23
  • change the above to a[szd] if you actually mean as or az or ad Commented Jan 24, 2015 at 15:30
  • Thanks guys. Most helpful and I will checkout regex101.com too! Commented Jan 24, 2015 at 15:31

2 Answers 2

3

I think you wrongly mean / as line start. Replace / with ^ (start of the line anchor).

regexp = re.compile(r'^[a-zA-Z0-9_.-]+$')

And note that [a-zA-Z0-9_] would be written as \w. So you could reduce the above regex to r'^[\w.-]+$'.

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

1 Comment

This worked! You sir are a genius. I will accept as soon as I can :)
3

Python regular expressions do not start with / like in some other languages. So, Python is trying to match a literal / character. You need to remove this:

regexp = re.compile(r'[a-zA-Z0-9_.-]+$')

Demo:

>>> import re
>>> word = 'mixed_CASE_word_00008908908808908080'
>>> regexp = re.compile(r'[a-zA-Z0-9_.-]+$')
>>> regexp.search(word)
<_sre.SRE_Match object; span=(0, 36), match='mixed_CASE_word_00008908908808908080'>
>>>

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.