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

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Regex to get string after last backslash (\).


    Hi,

    I have a file that I need to extract the string after the last backslash. Basically, the strings to find are:

    workingDirectory=c:\web\intra\Forms\BSEN
    workingDirectory=c:\web\intra\Forms\Mate
    workingDirectory=c:\web\intra\Forms\Parlam
    workingDirectory=c:\web\intra\Forms\Veto
    workingDirectory=c:\web\intra\Forms\Mate_arquivo
    workingDirectory=c:\web\intra\Forms\Disc
    workingDirectory=c:\web\intra\Forms\MenuGeral
    workingDirectory=c:\web\intra\Forms\Gescon
    workingDirectory=c:\web\intra\Forms\Conor
    workingDirectory=c:\web\intra\Forms\CPI_APOIO
    workingDirectory=c:\web\intra\Forms\ergon
    workingDirectory=c:\web\intra\Forms\conor_seep
    workingDirectory=c:\web\intra\Forms\qordem

    I would like some assitance to build the regular expression so I can get the last word after the last \ of each line.

    Thanks in advanced,
    Giancarlo
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,185
    Rep Power
    9398
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    Non-slash stuff with an end-of-line anchor.
    Hi,

    Thanks for reply. Bu I don't know how to get started. Would you mind if you write the regex for me? The final result would be:

    BSEN
    Mate
    Parlam
    Veto
    Mate_arquivo
    Disc
    MenuGeral
    Gescon
    Conor
    CPI_APOIO
    ergon
    conor_seep
    qordem


    Thanks again,
    Giancarlo
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,185
    Rep Power
    9398
    1. Use a negated character set [^...] and inside put a slash. Thus "everything but a slash".
    2. Repeat that character set using * or + (probably the latter).
    3. After it comes and end-of-line anchor $.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    Hi,

    I tried the following regex: [^workingDirectory=c:\\web\\intra\\Forms\\*]$
    but didn't work. What am I missing?

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

    Join Date
    Jun 2012
    Posts
    837
    Rep Power
    496
    You could try:

    Code:
    \\[^\]+\s*$
    (one backslash, followed by one or more non back-slashes, followed by optional spaces, followed by end of line.

    Or, yet simpler:

    Code:
    \w+\s*$
    one or more word characters (i.e. A-Z, a-z, 0-9 and _), followed by optional spaces, followed by end of line.

IMN logo majestic logo threadwatch logo seochat tools logo