Thread: RegEx help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    RegEx help


    I have below line

    ID: 12323 Name:Test

    I have below regexp
    ID:\s(?<value>.*)

    Which gives me 12323 Name:Test

    But i only want 12323

    Can anyone help modifying the regex plz

    regards
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    RegEx suggestion


    Try this:

    \b\d{5}\b

    Jim

    Originally Posted by Harris00
    I have below line

    ID: 12323 Name:Test

    I have below regexp
    ID:\s(?<value>.*)

    Which gives me 12323 Name:Test

    But i only want 12323

    Can anyone help modifying the regex plz

    regards
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,111
    Rep Power
    9398
    Could Name or Test be only numbers? Is that line in fact a real line or just a portion of the text?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0
    Originally Posted by requinix
    Could Name or Test be only numbers? Is that line in fact a real line or just a portion of the text?
    Thats full line, and it could be alphnumeric.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Revised


    Based upon what you said about the pattern being alphanumeric, try this instead. I'm assuming that the pattern you're looking for will always be immediately preceded by 'ID: '

    (?<=ID: )\b\w{5}\b

    Jim
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Originally Posted by jpietrangelo
    Based upon what you said about the pattern being alphanumeric, try this instead. I'm assuming that the pattern you're looking for will always be immediately preceded by 'ID: '

    (?<=ID: )\b\w{5}\b

    Jim
    The match will probably succeed, but this will not capture the matched number. You'll need to add parens to capture the content:

    Code:
    (?<=ID: )\b(\w{5})\
    b
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,111
    Rep Power
    9398
    Originally Posted by Laurent_R
    The match will probably succeed, but this will not capture the matched number. You'll need to add parens to capture the content:
    Not necessarily. Most languages I can think of will capture not just parenthesized groups but the entire portion of the string matched. Namely $0. The way jpietrangelo wrote the expression, $0 will be the same as your $1.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Originally Posted by requinix
    Not necessarily. Most languages I can think of will capture not just parenthesized groups but the entire portion of the string matched. Namely $0. The way jpietrangelo wrote the expression, $0 will be the same as your $1.
    OK, I did not think of it in these terms.

    I still tend to think that it is probably better practice to explicitly specify what part of the match you want to capture, rather than relying on an implicit capture of the whole match.

IMN logo majestic logo threadwatch logo seochat tools logo