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

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Need to find value after 3 dashes


    This expression

    [A-Z]+(?=-\d+$)

    find SS and BCP from following string

    ANG-B31-OPS-PMR-MACE-SS-0229
    ANG-RGN-SOR-BCP-0004

    what I want to do is find the value after third "-" which is
    PMR in first string and BCP in second string

    any help will be highly appreciated
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    If the string contains an indeterminate number of sections and you always want to match the fourth one from the beginning, then it's better to write your regular expression so that it matches from the beginning of the string rather than the end.

    If you can extract a subgroup from the match, then write an expression which matches the first four sections as separate subgroups and pull out the 4th one.

    If you can't extract a subgroup, check to see whether the regular expression engine that you're using supports zero-width look-behinds.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    834
    Rep Power
    496
    An easy solution in Perl:

    Perl Code:
    my $c = "ANG-B31-OPS-PMR-MACE-SS-0229"
    my ($d, $e) = (split /-/, $c) [3,-2];
    print "$d, $e"; # prints PMR, SS


    It will be slightly different in other languages, perhaps very slightly more complicated than just one short line of code as above, but your language is likely to have a split function that will be the basis of a similar solution.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    This is one to do it in a shell script:
    Code:
    $ cat search_example.sh
    #!/usr/bin/bash
    oIFS=$IFS  ## Save current setting
    IFS="-"
    while read -a Arr theFile
    do
      echo ${Arr[3]}
    done < t
    IFS=$oIFS
    
    $ cat t
    ANG-B31-OPS-PMR-MACE-SS-0229
    ANG-RGN-SOR-BCP-0004
    
    $ search_example.sh
    PMR
    BCP

IMN logo majestic logo threadwatch logo seochat tools logo