Home > OS >  Regex - how to include URL, up to non-alphabet characters or certain characters
Regex - how to include URL, up to non-alphabet characters or certain characters

Time:12-01

I'm trying to get a regex that captures the handle of Twitter account. For example if URL is https://twitter.com/example, I want to capture "example".

I was able to do so with this: (?<=twitter.com.)(.*)

However there is an issue, if other characters come after it.

For example if the URL is : https://twitter.com/example/ or https://twitter.com/example?abc=xyz

Now it captures / and everything after ? as well.

How can I capture just the handle, and stop when there are invalid characters? There can be _ in the handle as well, so _ cannot stop the regex.

Hope that makes sense.. Thank you in advance.

(?<=twitter.com.)(.*)

I wanted to capture just the Twitter handle, but it also captures other characters such as "/" or "?abc=xyz"

CodePudding user response:

The .* is too greedy. You need to be more specific or excluding ([^...]) the parameters in general:

(?<=twitter.com.)([^?]*)

I think the above does what you wanted.

  • Related