Reputation: 109
I would like to match the fragment portion of a url, meaning a sub-string that begins with '#'.
A URL should not have a fragment and a query. So, if a '?' precedes the first '#', the sub-string "#..." should not match.
example of url(s) with fragment:
http://exampleurl.com:8080/#this?whole#thing?is#a?fragment
http://exampleurl.com:8080#this?whole#thing?is#a?fragment
http://exampleurl.com#this?whole#thing?is#a?fragment
example of url with no fragment:
http://exampleurl.com:8080/?#this?whole#thing?is#NOT?a#fragment
http://exampleurl.com:8080/?this?whole#thing?is#NOT?a#fragment
The regular expression should match "#this?whole#thing?is#a?fragment"
in the first example, but should not match anything in the second example. As stated before, if there is a '?' preceding the first '#', then it will be considered a query and not a fragment.
attempt:
"#+.+$"
The above regex matches correctly in regards to the first example, but it will also match the sub-string "#thing?is#NOT?a#fragment"
in the second example which is not desirable. I am not sure how to put in the proper guards to prevent it from matching with a sub-string that has a '?' preceding it.
Thanks!
Upvotes: 1
Views: 151
Reputation: 72
This one matched all your cases correctly. Result is in first group.
/^[^#?]*(#.*)/gm
check results here https://regex101.com/r/p6tsh2/1
Upvotes: 2