Regex to exclude first and last characters of match

10,866

Solution 1

You can also use positive lookahead and lookbehind

(?<=_)\w*2(?=_)

My intention is to extract only 'test2'. Using \w*? as string match, I can get 'word2' as a match, but I can't see a way of removing those underscores to match only 'word2'.

The underscores won't be part of the matching string but will be before and after it

EDIT:

Going further, if the match string is on the beginning or end of the whole text, it won't be surrounded by underscores.

(?<=_|^)[^_]*2(?=_|$)

This one makes optional the use of underscore in this specific situation.

online test

Solution 2

Your question isn't entirely clear, but assuming that word2, word3, etc are arbitrary words which don't contain _, you can use capturing groups to extract a subset of the characters that want to extract. For example:

\w*_(\w*)_\w*_\w*

That matches your string and the first (and only) capture group extracts the second word. Maybe the * should be + depending on whether you want to accept zero-sized words.

Share:
10,866
Sergio Figueras
Author by

Sergio Figueras

Updated on June 23, 2022

Comments

  • Sergio Figueras
    Sergio Figueras almost 2 years

    I've the following string: word_word2_word3_word4

    My intention is to extract only 'test2'. Using _\w*?_ as string match, I can get 'word2' as match, but I can't see a way of removing those underscores to match only 'word2'.

    I can't use .split() or something like it, this value must be gathered using Regex only.

    What modifications do you suggest guys?

  • Sergio Figueras
    Sergio Figueras over 7 years
    Hello @BeeOnRope, thanks for your suggestion. Unfortunately if I use your regex it matches all the string. I just want the "word2" without any underscores. Do you have any modification on it?
  • Sergio Figueras
    Sergio Figueras over 7 years
    It cant be done because it the clients must be able to configure it in anyway that they want. This is one string that I need to match only.
  • BeeOnRope
    BeeOnRope over 7 years
    What do you mean by want? The code above matches the entire string, but gives you only word2 when you query for the first matched group (the part (\w*)). If you care about matching you could use a combination of lookahead and lookbehind.
  • Sergio Figueras
    Sergio Figueras over 7 years
    I've used regexr.com , applied this regex with word1_word2_word3_word4 string and it matches all of this, not only word2 .
  • Sergio Figueras
    Sergio Figueras over 7 years
    Worked like a charm! Thanks! :) :)
  • anubhava
    anubhava over 7 years
    Why do you have 2 in your regex. What if input is abc_foo_pqr_bar?
  • Caio Oliveira
    Caio Oliveira over 7 years
    The user asked for matching a text with the 2 enclosed (test2). If the case were abc_foo_pqr_bar then would be no matches...