Regex to exclude first and last characters of match
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.
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.
Sergio Figueras
Updated on June 23, 2022Comments
-
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 over 7 yearsHello @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 over 7 yearsIt 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 over 7 yearsWhat 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 over 7 yearsI've used regexr.com , applied this regex with word1_word2_word3_word4 string and it matches all of this, not only word2 .
-
Sergio Figueras over 7 yearsWorked like a charm! Thanks! :) :)
-
anubhava over 7 yearsWhy do you have
2
in your regex. What if input isabc_foo_pqr_bar
? -
Caio Oliveira over 7 yearsThe user asked for matching a text with the
2
enclosed (test2
). If the case wereabc_foo_pqr_bar
then would be no matches...