-4

Having a hard time with Regex. What would be the regex for finding a file name with variable in between them? For eg:

File name : DON_2010_JOE_1222022.txt

In the above file name the words DON, JOE and the format .txt will remain constant. Rest numbers might change for every file. There could be characters as well instead of numbers in those two places. What im looking for is basically something like DON_*_JOE_*.txt with * being whatever it could be.

Can someone please help me with this?

I tried DON_*_JOE_*.txt and obviously it did not work.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Kevin John
  • 23
  • 3
  • `DON_([0-9]+)_JOE_([0-9]+)\.txt` should work. Getting the values from these capture groups in java should be doable. – f1sh Dec 02 '22 at 18:10
  • In Java, you can use `DON_\p{Alnum}+_JOE_\p{Alnum}+\.txt`. Check [this](https://regex101.com/r/IJbzf1/1). – Arvind Kumar Avinash Dec 02 '22 at 18:16
  • "*" matches the previous token between zero and unlimited times, as many times as possible – mahipalkeizer Dec 02 '22 at 18:35
  • `DON_.{4}_JOE_.{7}\\.txt` will work. You can use [regex101](https://regex101.com) in future for regex related issues and do basic debuggig on your ow – mahipalkeizer Dec 02 '22 at 18:38
  • DON_([0-9]+)_JOE_([0-9]+)\.txt Worked for me.. THanks a lot, f1sh(https://stackoverflow.com/users/214525/f1sh) for the comment. – Kevin John Dec 03 '22 at 21:17

2 Answers2

0
DON_(?<firstString>.*)_JOE_(?<secondString>.*).txt

You can use this. To access the specific group, you can use matcher.group("firstString").

-1

In JavaScript:
"DON_2010_JOE_1222022.txt".match(/DON_.+_JOE_.+\.txt/)

whatever it could be

It is .+ except new lines.

oleedd
  • 388
  • 2
  • 15