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

    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Newbie Alert -First Regex attempt


    Okay, let the flaming begin - here's what I have:

    String Example:
    DUNS-NUMBER 1( 9)
    COMPANY-NAME 10( 30)
    SECONDARY-NAME 40( 30)

    that goes for 180 lines or so. I want to be able to break out the number in the parentheses - which can be up to a 3 character numeric. Based on what I've looked at for regfex statements, this is what I've come up with:

    /\(^[0-9]{1,3}\)$/

    Of course, not working thus far. I know I have to account for the spaces in the string - but I don't know whether it's a space or a tab. I know that \s and \t handle those special characters, and that some combination of \s|\t will need to be used as well. Ultimately, the result I'd like to see from the first few strings would be:

    9
    30
    30
    ETC....

    Can someone give the newbie a push in the right direction?

    Thanks,
    Sean
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Get rid of the ^, it only matches at the beginning on a line.

    \s is a character class that includes (among others) both the space and tab characters. If you want to match some whitespace and don't care what it actually is, use \s*

    (BTW, you can use \d in place of [0-9] for simplicity unless you're worried about weird unicode digits, similarly I'd use + instead of {1,3} unless there was a specific need to only pull out lines with 3 or fewer digits)
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    What language are you using? For a general=purpose MATCH expression, you want:
    /\(\s*(\d+)\)/

    That will put your number in \\1, $1, $matches[1], etc. depending on your language.

    For a replace expression, try:
    s/^.+\(\s*(\d+)\)$/\\1/

    That, when used in sed or perl, will replace each line with the number in parens. The PHP equivalent is:
    echo preg_replace("/^.+\(\s*(\d+)\)$/", "\\1", $yourString);

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Thanks for the help guys


    it's PERL, btw. I appreciate the assistance.

    Sean

IMN logo majestic logo threadwatch logo seochat tools logo