1

Hi just when I think I understand Regex im slapped across the face with a wet fish. Can anyone help me out here, i'm almost there but just cant quite figure the last part out.

Using the following URLs to test against and what im trying to match using the regex:

URL                                        === Regex needs to match
---------------------------------------------------------------------
http://localhost#stq=textarea&c=all        === http://localhost
http://localhost?#stq=textarea&c=all       === http://localhost?
http://localhost/#stq=textarea&c=all       === http://localhost/
http://localhost/?#stq=textarea&c=all      === http://localhost/?

http://localhostq=textarea&c=all           === http://localhost
http://localhost?q=textarea&c=all          === http://localhost?
http://localhost/q=textarea&c=all          === http://localhost/
http://localhost/?q=textarea&c=all         === http://localhost/?

http://localhost/test#stq=textarea&c=all   === http://localhost/test
http://localhost/test?#stq=textarea&c=all  === http://localhost/test?
http://localhost/test/#stq=textarea&c=all  === http://localhost/test/
http://localhost/test/?#stq=textarea&c=all === http://localhost/test/?

http://localhost/testq=textarea&c=all      === http://localhost/test
http://localhost/test?q=textarea&c=all     === http://localhost/test?
http://localhost/test/q=textarea&c=all     === http://localhost/test/
http://localhost/test/?q=textarea&c=all    === http://localhost/test/?

The #stq param can be changed so it's not always going to start with a #. Also the domain isn't always going to be localhost and there may or may not be a pathname.

So far what my RegEx so far but its not picking up if theres a pathname

^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i

Ive created an example here: https://regex101.com/r/TInZCN/2

Any help would be awesome!

4
  • Do you wanna separate out the domain name and the remaining parameters? Can you give your desired output for each of the examples given? Commented Jul 1, 2020 at 5:15
  • If I can do that that would be even better. Currently ive using the regex to identify the host and pathname (if preset) then remove it so im just left with the parameters, which im then looping through to map search fields. So with the example URLs this is what im trying to match from each URL (which should just leave me with the params once ive removed the domain / pathname) : Commented Jul 1, 2020 at 5:45
  • Why not use var uri = new System.Uri("http://localhost/test/#stq=textarea&c=all"); and then access uri.Fragment? Commented Jul 1, 2020 at 6:15
  • I cant use uri.Fragment as the # on the param might not be present in the string, that param can be entered as q=.... or even query=.... Commented Jul 1, 2020 at 6:52

2 Answers 2

2

You might use

^https?:\/\/(?:(?!q=)[^\n?=#])+\??

In parts

  • ^ Start of string
  • https?:\/\/ Match the protocol with optional s
  • (?:(?!q=)[^\n?=#])+ Match any char except one of \n?=# and not followed by q=
  • \?? Match an optional ?

Regex demo


If the # or q= is always present, you might use a non greedy match followed by asserting either # or q=

^https?:\/\/.*?(?=#|q=)

Regex demo

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

1 Comment

Amazing thank you so much, thats exactly what im looking for!!!
-1
(?<=http:\/\/localhost[\/|\?]).*

This is an exclusion to remove to remove local host (or your domain) and match everything after that.

1 Comment

This is capturing the path which the OP said they didn't want to capture.

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.