-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

sdgd
  • 723
  • 1
  • 17
  • 38

1 Answers1

2

It is definitely possible using lookarounds:

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

Demo

PJProudhon
  • 835
  • 15
  • 17
  • `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 – sdgd Nov 28 '17 at 09:22
  • found the issue, one more `\\` is required – sdgd Nov 28 '17 at 09:26
  • thanks PJ. this worked like a charm!! – sdgd Nov 28 '17 at 09:57