I have this regex right now:
file:\s?['"](.*?)['"]
For this string:
file: "word.124-ew/ef/?s=sf"
And the output is:
file: word.124-ew/ef/?s=sf
But I would also like to capture a group that hasn't double quotes (optional). For example, this:
file: word.124-ew/ef/?s=sf
No double quotes, but can also have them (optional).
I tried making this regex, but it hasn't the result desired:
file:\s?['"]?(.*)['"]?
And the output is:
word.124-ew/ef/?s=sf
With a final double quotes included, and its not what I expected (not to capture the double quotes).
Now with the last Regex I made, I have a problem when adding double quotes again. The output is:
file: word.124-ew/ef/?s=sf"
Tested with:
http://regexr.com/
RegExp: file:\s?['"]?(.*)['"]?
String1: file: "word.124-ew/ef/?s=sf"
String2: file: word.124-ew/ef/?s=sf
Fails on: file: word.124-ew/ef/?s=sf" (captures the final double quote and I don't want to capture the double quotes)
Any solutions? I will be glad if you guys can land me a hand! Thanks
You can use the following regex:
file:\s*['"]?([^'"]+)
See the regex demo (note that \n is added for the demo purposes only)
I used \s* to match 0 or more whitespace (if there is more than 1, it will match them all), added the ? quantifier to make ['"] optional, and turned the lazy dot matching subpattern into a negated character class ([^'"]+) matching 1 or more characters other than ' and ".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With