tags:

views:

189

answers:

5

I would like to intercept string starting with *#*

followed by a number between 0 and 7

and ending with: ##

so something like *#*0##

but I could not find a regex for this

+1  A: 

r'\#[0-7]\#\#'

Alex Martelli
I'm curious about escaping the '#'s. As I read the docs, the '#' is special only when re.VERBOSE is specified or in the (?#...) construct.
Peter Rowell
Yes, but it's best practice in RE patterns to escape all non-alphamerics that are being used literally -- safeguard against "upgrades" to the RE pattern language that might break non-escaped alphamerics, plus, help to the readers by letting them know that they need not look up whether some weird unescaped punctuation char has special meaning in a RE pattern;-).
Alex Martelli
+1  A: 

The regular expression should be like ^#[0-7]##$

Blindy
+7  A: 

Assuming you want to allow only one # before and two after, I'd do it like this:

r'^(\#{1}([0-7])\#{2})'

It's important to note that Alex's regex will also match things like

###7######
########1###

which may or may not matter.

My regex above matches a string starting with #[0-7]## and ignores the end of the string. You could tack a $ onto the end if you wanted it to match only if that's the entire line.

The first backreference gives you the entire #<number>## string and the second backreference gives you the number inside the #.

Mark Biek
+1 for noticing that
JPCosta
r'^\*{1}\#{1}\*{1}([0-7])\#{2}$' for *#*0## ? Is it right?
DrFalk3n
@DrFalk3n, yes that looks like it works
Mark Biek
+3  A: 

None of the above examples are taking into account the *#*

^\*#\*[0-7]##$

Pass : *#*7##

Fail : *#*22324324##

Fail : *#3232#

The ^ character will match the start of the string, \* will match a single asterisk, the # characters do not need to be escape in this example, and finally the [0-7] will only match a single character between 0 and 7.

Doomspork
Whoops. I think I assumed those * were mistyped Markdown syntax
Mark Biek
+1  A: 

As I understand the question, the simplest regular expression you need is:

rex= re.compile(r'^\*#\*([0-7])##$')

The {1} constructs are redundant. After doing rex.match (or rex.search, but it's not necessary here), .group(1) of the match object contains the digit given.

EDIT: The whole matched string is always available as match.group(0). If all you need is the complete string, drop any parentheses in the regular expression:

rex= re.compile(r'^\*#\*[0-7]##$')
ΤΖΩΤΖΙΟΥ
really I need all the matched string not only the single digit, but your suggestion is very usefull thanks
DrFalk3n