-2

I want to write 3 different regular expressions. The first should detect ${ANY SEQUENCE OF CHARACTERS}, the second should detect ${ANY SEQUENCE OF CHARACTERS=ANY SEQUENCE OF CHARACTERS}, and the third should detect either ${ANY SEQUENCE OF NUMBERS} or $ANY SEQUENCE OF NUMBERS

Here is what I have tried:

import sys
import os
import re

ncmd = 1
regular = re.compile('${[a-z]*}')
equals = re.compile('${*[=]*}')      # string followed by equals followed by string
numbers = re.compile('${(0-9)*}|$(0-9)*') # ${ANY_SEQUENCE_OF_DIGITS} or $ANY_SEQUENCE_OF_DIGITS

while(1):  #print line
    line = raw_input("(%s)$ " % ncmd)
    if regular.match(line):
        print "Regular"
        print(os.getenv(line[1:],''))
    elif equals.match(line):
        print "Equals"
    elif numbers.match(line):
        print "Numbers"
    else:
        print(line)
    ncmd += 1
Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
Apollo
  • 8,874
  • 32
  • 104
  • 192
  • 1
    Perhaps you haven't noticed that dollar signs and curly braces have special meanings in reg-ex? – Michael Lorton Oct 10 '14 at 21:31
  • @Malvolio thanks that's what I was missing! – Apollo Oct 10 '14 at 21:34
  • 2
    Don't put answers in the question – jonrsharpe Oct 10 '14 at 21:37
  • Either you or @Malvolio should post an answer if it indeed solved your problem. – Farmer Joe Oct 10 '14 at 21:39
  • possible duplicate of [What special characters must be escaped in regular expressions?](http://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions) – Air Oct 10 '14 at 21:43
  • That's not the only thing you're missing. `*` isn't an expression on its own; it just _modifies the preceding expression_. If you want to match any number of anything, you need `.*`. But even that isn't right, because I don't think you want to match 0 characters, so you need `.+`. And even that probably isn't right, because you probably want to match everything up to the first `=`, not up to the last `=` that lets the rest of the pattern match, so you probably need `.+?`. – abarnert Oct 10 '14 at 22:15
  • Also, `(0-9)` isn't any digit; it's the string `'0-9'`, as a capturing group. You probably wanted `[0-9]` there. I wouldn't be surprised if there were other elementary errors as well. You need to read a good primer on regular expressions (the reference docs are helpful, but not when you're first learning…), and you should play with a nice regex debugger/explorer tool like [Debuggex](https://www.debuggex.com/) to get the hang of it. – abarnert Oct 10 '14 at 22:16

1 Answers1

1

The dollar sign, by itself, means "end of line". Consider the following code:

>>> import re
>>> print re.compile('${[a-z]*}').match("${abcd}")
None
>>> print re.compile('\${[a-z]*}').match("${abcd}")
<_sre.SRE_Match object at 0x109fc51d0>

The first regexp is look for an end-of-line at the beginning of the line, so... no. The backslash-dollar sign means "No, I mean, literally, match a dollar sign."

The curly braces have special meanings too, but not here.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144