0

Examples of filenames

  1. FDIP_en-gb-nn_Text_v1_YYYYMMDD_SequenceNumber.txt

  2. FDIP_fr-fr-nn_Text_v1_YYYYMMDD_SequenceNumber.txt

  3. FDIP_de-de-nn_Text_v1_YYYYMMDD_SequenceNumber.txt

REGEX is FDIP_([a-z]{2}-[A-Z]{2}-[a-z]{2})_Text_v1_[0-9]{8}_[0-9]{14}.txt

The only part I need is the translation code which is 'en-gb', 'fr-fr' , 'de-de.

How do I extract just that part of the filename?

KaiserKatze
  • 1,521
  • 2
  • 20
  • 30
Shallesse
  • 189
  • 1
  • 1
  • 6
  • try using `group name` to extract that – Kaushik Nov 13 '19 at 11:11
  • Possible duplicate of [How to find indices of groups in JavaScript regular expressions match?](https://stackoverflow.com/questions/1985594/how-to-find-indices-of-groups-in-javascript-regular-expressions-match) – Estradiaz Nov 13 '19 at 11:12
  • I'm assuming you're running it with case-insensitive (otherwise the `[A-Z]{2}` would fail your examples). In which case, just move the bracket forward (so it's `([a-z{2}-[A-Z]{2})` and then use the first group returned – freefaller Nov 13 '19 at 11:14

3 Answers3

1

Modified the regex little bit to match the numbers and text. You can play around here

Explanation to capture a group you need to wrap the regex into () this will capture as a group.

to do the named capturing you can (?<name_of_group>) and then you can access by name.

Here goes the matching process.

[a-z]{2} match 2 char from a-z

[a-zA-Z0-9] match any char of a-z or A-Z or 0-9

g means global flag i.e. match all.

i means ignore case.

var r = /FDIP_([a-z]{2}-[A-Z]{2})-[a-z]{2}_Text_v1_[0-9A-Z]{8}_[A-Z0-9]{14}.txt/gi;

let t = 'FDIP_en-gb-nn_Text_v1_YYYYMMDD_SequenceNumber.txt';

let dd = r.exec(t);


console.log(dd[1]);

This is example of group capturing

See the name in the regex and the object destructing name is matching.

const { groups: { language } } = /FDIP_(?<language>[a-z]{2}-[A-Z]{2})-[a-z]{2}_Text_v1_[0-9A-Z]{8}_[A-Z0-9]{14}.txt/gi.exec('FDIP_en-gb-nn_Text_v1_YYYYMMDD_SequenceNumber.txt');

console.log(language);
Kaushik
  • 2,072
  • 1
  • 23
  • 31
  • It's better to explain exactly what you've changed.. otherwise you're expecting people to search through a long regex looking for tiny changes (in this case moving a single `)` character) – freefaller Nov 13 '19 at 11:23
  • @freefaller Thanks for pointing out. I've added the explanation. Let me know if missing something. – Kaushik Nov 13 '19 at 11:30
  • thank you for the feedback what if there is many types of filenames that will be loaded as stated do i create an array where r is – Shallesse Nov 13 '19 at 11:44
  • You can use multi line flag `m` in the regex if you want to match multiple lines. – Kaushik Nov 14 '19 at 03:58
0

Change FDIP_([a-z]{2}-[A-Z]{2}-[a-z]{2})_Text_v1_[0-9]{8}_[0-9]{14}.txt

to

let pattern = /FDIP_([a-z]{2}-[a-z]{2})-[a-z]{2}_Text_v1_[\w]{8}_[\w]{14}.txt/;

var str = 'FDIP_en-gb-nn_Text_v1_YYYYMMDD_SequenceNumber.txt';
console.log(str.match(pattern)[1]);
LF00
  • 27,015
  • 29
  • 156
  • 295
0

To solve your problem, you should:

  • Fix your regex:
FDIP_([a-z]{2}-[A-Z]{2}-[a-z]{2})_Text_v1_[0-9]{8}_[0-9]{14}.txt
// to
FDIP_([a-z]{2}-[a-z]{2})-[a-z]{2}_Text_v1_[0-9]{8}_[0-9]{14}.txt
  • Use get value from first group by using regex.exec function

const fileNames = [
'FDIP_en-gb-nn_Text_v1_20190101_12345678901234.txt',
'FDIP_fr-fr-nn_Text_v1_20200202_12345678901234.txt',
'FDIP_de-de-nn_Text_v1_20180808_12345678901234.txt']

const cultureNames = fileNames.map(name => {
  const matched = /FDIP_([a-z]{2}-[a-z]{2})-[a-z]{2}_Text_v1_[0-9]{8}_[0-9]{14}.txt/.exec(name)
  
  return matched && matched[1]
})

console.log(cultureNames)
Long Nguyen Duc
  • 1,317
  • 1
  • 8
  • 13
  • It's better to explain exactly what you've changed.. otherwise you're expecting people to search through a long regex looking for tiny changes (in this case moving a single `)` character) – freefaller Nov 13 '19 at 11:23