Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 10 of 10

Thread: Python Regex

Hybrid View

  1. #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.

    Code:
    ^([a-zA-Z]{1,}) ?(?:(?:-((?:[a-zA-Z0-9]){1,}) ?){,})?$
    For anyone who actually does look through that, here's my problem:

    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:

    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
    Thanks for the help!
    [Ex] Proud Montanadian

  2. #2
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    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 . . .

  3. #3
    Quote Originally Posted by TheCanadian View Post
    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.
    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:
    Code:
    commandname -arg1 -arg2 (as many args as you'd like, named whatever)
    2nd:
    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

  4. #4
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    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 . . .

  5. #5
    Quote Originally Posted by TheCanadian View Post
    So "commandname -arg1 -arg2" is your string and all arguments are preceded by a hyphen and commandname can be any alpha-numeric word?
    Yup, looks right. The spaces between arguments are also optional.
    [Ex] Proud Montanadian

  6. #6
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    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+ *)+$/i
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  7. #7
    Quote Originally Posted by TheCanadian View Post
    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+ *)+$/i
    Alright, I was wondering if two regular expressions was the way to go.

    Thanks for your help!
    [Ex] Proud Montanadian

  8. #8
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    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 . . .

  9. #9
    [Ex] Proud Montanadian

  10. #10
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    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 . . .

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012