Issue
I am extracting usernames from urls using ^(?:https?://)?(?:www.)?(?:website.com)?/?([^/\s]+)
. This method will return "username?lang=en". How can i remove the "?lang=en" from the end ?
Solution
You can use
^(?:https?://)?(?:www\.)?(?:website\.com)?/?([^/\s?#]+)
See the regex demo. Details:
^
- start of string(?:https?://)?
- an optional occurrence ofhttp://
orhttps://
(?:www\.)?
- an optional occurrence ofwww.
(note that the literal dot must be escaped)(?:website\.com)?
- an optional occurrence ofwebsite.com
/?
- an optional/
char([^/\s?#]+)
- Group 1: one or more chars other than/
, whitespace,?
and#
chars.
Answered By - Wiktor Stribiżew
Answer Checked By - Mary Flores (JavaFixing Volunteer)