-1

I have files names in the below format -

India_AP_Dev1.txt
USA_GA_QA2.txt
USA_NY_AWSDev1.txt
AUS_AA_BB_QA4.txt

I want to extract only the environment part from the file name i.e. Dev1, QA2, AWSDev1, QA4etc. How can I go about with this type of file names. I thought about substring but the environment length is not constant. Is it possible to do it with regex

Appreciate your help. TIA

0

1 Answer 1

2

It is definitely possible using lookarounds:

(?<=_)[^._]*(?=\.)
  • (?<=_) match is preceded by _
  • [^._] take all characters except . and _
  • (?=\.) match is followed by .

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

def regexformat = "(?<=_)[^._]*(?=\.)" Matcher digitMatcher = Pattern.compile(regexformat).matcher(jsoname); i am trying out this way but i see the error unexpected char: '\'. any suggestions please. jsoname will have my file name
found the issue, one more `\` is required
thanks PJ. this worked like a charm!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.