Seraf
Seraf

Reputation: 109

Javascript regex : repeating non-capturing group

I'm working on a regex, but I can't make it work as I'd like.

The strings, 2 examples :

- /download?standard=yes&file=France_new-aquitaine_deux-sevres_europe_2.obf.zip

- /download?standard=yes&file=Afghanistan_asia_2.obf.zip

I want to extract following parts :

- country (France)  
- region (new-aquitaine)  
- department (deux-sèvres)  
- worldZone (europe)  

My WIP regex :
/.*file=(?:(.*?)_{1})*?(?:\d\.obf\.zip)$/gi

Maybe there is a recursive way to handle it, I don't know.

Can you help or guide me ? Thanks.

Solved with :

/.*file=([^_]+)_(?:([^_]+)_)??(?:([^_]+)_)??([^_]+)_2\.obf\.zip$/g

Upvotes: 0

Views: 146

Answers (1)

The fourth bird
The fourth bird

Reputation: 163632

One option could be using capturing groups and if you want to match the country and the region for the second string as well, you could make that part optional using an optional non capturing group (?:...)?

The parts are separated by an underscore. You could use a negated character class ([^_]+) matching any char except an underscore to capture the parts in between.

.*file=([^_]+)_([^_]+)(?:_([^_]+)_([^_]+))?_\d+\.obf\.zip$

Regex demo

Upvotes: 2

Related Questions