0

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

1
  • This part: (?:(.*?)_{1})*? will only match the last occurence in a group. Instead separate the groups, you want to match, then you can access the captured groups (by index). Commented Mar 25, 2020 at 16:56

1 Answer 1

2

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

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

1 Comment

Thanks for your working answer. I've improved a little : .*file=([^_]+)_([^_]+)(?:_([^_]+)_([^_]+))?_\d+\.obf\.zip$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.