Regex group match if present

My input string is:

/234243/source_path/a/b/c.test

or something like:

/234243/source_path/a/b/c.test/check_w123

I want a regex to match substrings starting with source and check with the result like:

using a regex like /(?<source>source.*)(?<check>check.*)/ without ? in the last group.

My regex is:

/(?<source>source.*)(?<check>check.*)?/

My Result is:

Upvotes: 0

Views: 299

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

Just turn .* inside the first source group into it's non-greedy form. And don't forget to add end of the line anchor.

(?<source>source.*?)(?<check>check.*)?$

DEMO

Upvotes: 2

vks
vks

Reputation: 67968

(?<source>source.*?(?!.*\/))(?<check>check.*)?

Try this.See demo.

https://regex101.com/r/rU8yP6/11

Upvotes: 0

Related Questions