Results 1 to 2 of 2

Thread: RegExp question

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default RegExp question

    Hi,

    I want to match the following cases:
    somename-123
    somename_123
    Where 'somename' should be any string of letters (any case) then a '-' OR a '_' and then optionally a number.
    Here is my RegExp:
    [a-zA-Z]+_|\-[a-zA-Z]+[0-9]

    But it only matches 'somename_'. not 'somename-'
    If switch between '_' and '-' so:
    [a-zA-Z]+\-|_[a-zA-Z]+[0-9]
    then only 'somename-' will match - so only the first between then two (\-|_) will match.
    Why?

    I tried also
    [a-zA-Z]+(_|\-)[a-zA-Z]+[0-9] which matches nothing then.

    Thanks in advance.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: RegExp question

    Quote Originally Posted by high_flyer View Post
    [a-zA-Z]+_|\-[a-zA-Z]+[0-9]
    This means: match (one or more letters followed by "_") or ("-" followed by one or more letters and a digit).

    What you need is: [a-zA-Z]+((_|-)[0-9]+)?
    I.e. match one or more letters optionally followed by (("_" or "-" ) and one or more digits).

    A bit more efficient version looks like this: [a-zA-Z]+(?:(?:_|-)[0-9]+)? (since you don't want to capture anything). You can write it also this way: [a-zA-Z]+([-_][0-9]+)? ("-" must be first).

    If you have any doubts, try kregexpeditor. It displays regexps in a semigraphical form, so you can see what they mean exactly.

  3. The following user says thank you to jacek for this useful post:

    high_flyer (26th August 2007)

Similar Threads

  1. Quick RegExp question
    By stealth86 in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2007, 08:23
  2. Replies: 1
    Last Post: 15th March 2007, 20:45
  3. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 15:34
  4. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.