Results 1 to 10 of 10
Thread: Python Regex
Hybrid View
-
January 24th, 2011, 05:22 PM #1
Python Regex
So, I'm working on a program, and am trying to use regular expressions to check some input to a commandline type system.
I've gotten pretty close to nailing it, but something is wrong.
For anyone who actually does look through that, here's my problem:Code:^([a-zA-Z]{1,}) ?(?:(?:-((?:[a-zA-Z0-9]){1,}) ?){,})?$
When I enter something, say "commandname", it works, and returns commandname as a group (due to being enclosed in parentheses).
When I try to enter something with arguments ( commandname -arg -arg -arg ...), it returns only the last argument.
Here's the function in question:
Thanks for the help!Code:def ProcessCommand(command): temp = '^([a-zA-Z]{1,}) ?(?:(?:-((?:[a-zA-Z0-9]){1,}) ?){,})?$' m = re.match(temp,com) if m == None: print('Not a recognized command.') return command = m.groups() name = command[0] # args should be in the rest of the "command" tuple[Ex] Proud Montanadian
-
January 24th, 2011, 05:35 PM #2
I don't know python but I do know regex so I could maybe help you if you help me understand what exactly your trying to do.
Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .
-
January 24th, 2011, 05:49 PM #3
Alright, so, here we go

I ask for input, call it the var "command".
I then am trying to do two things,
1st:
Check to make sure "command" follows a pattern:
2nd:Code:commandname -arg1 -arg2 (as many args as you'd like, named whatever)
Python has a function groups(), which returns a set of the data grouped in the Regular Expression. Out of this I want to get the command name, and a subset containing the supplied arguments (if any).
Tell me if you need more!
Thanks for the speedy response.[Ex] Proud Montanadian
-
January 24th, 2011, 06:02 PM #4
So "commandname -arg1 -arg2" is your string and all arguments are preceded by a hyphen and commandname can be any alpha-numeric word?
Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .
-
January 24th, 2011, 06:19 PM #5
-
January 24th, 2011, 07:07 PM #6
I don't know if that's possible just due to how regular expressions work but you could find the arguments in a separate regex. Something like this should work: /-\w+/g
Also, I think this regex does the same thing as yours but it's a little bit simpler:
/^([a-z]+) *(-\w+ *)+$/iProud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .
-
January 24th, 2011, 09:28 PM #7
-
January 25th, 2011, 03:57 PM #8
No thank you. Now I can say I'm a python expert!
Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .
-
January 25th, 2011, 05:10 PM #9[Ex] Proud Montanadian
-
January 25th, 2011, 05:36 PM #10
Haha I could easily do that now.
Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .

Reply With Quote


Bookmarks