I can't ever get Regex Right [duplicate] - regex

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I have a query string I'm trying to remove a query string parameter. What I currently have is
date=(.*)&
But this will remove anything to the last &. I only want to remove to the first occurance of the &.
So this is how it should work:
date=5%2F13%2F16&tab=1&a=b = tab=1&a=b
But mine is doing this:
date=5%2F13%2F16&tab=1&a=b = a=b
I think I probably need something around the & but I read the regex stuff and I just get more confused.

Make it lazy, use:
date=(.*?)&

Related

How to match all strings that has only one dot using regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to capture strings containing only one dot. String will mostly contains domain names like
test.com, fun.test.com, lesh.test.com.
I need to check only the first one and to ignore the string that has more than one dots.
How can I do this using regex?
Like this :
^[^.]+\.[^.]+$
Check explanations https://regex101.com/r/mn7Ccr/1

REGEX must include substring [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
How to extract a substring using regex
(14 answers)
Closed 3 years ago.
I'm trying to find the word: <*hasburnt*> in the string below using the this regex: <\*.*(bur).*\*>
But it gives me both <*hasburnt*> <*electrical*>. How do I just get <*hasburnt*> ?
bench testedstarter, starter just makes noise, and <*hasburnt*>
<*electrical*> smell.
Try this: /<.*?(bur).*?>/
Regex101 demo
The reason for ? here is because .* tries to match as much characters as possible, so it also matches <electrical. .*? makes it lazy - trying to match as little as possible, and as such ending the match at <hasburnt>.
EDIT: using ? for the first .* would make <hasburnt> independent of positions of similar strings.

Capture anything between first [] not all the [] in string [duplicate]

This question already has answers here:
What is the meaning of the 'g' flag in regular expressions?
(10 answers)
Closed 4 years ago.
I have a string like [only this] i want [to] [capture].
I want to have only this. I have tried \[.*?\] regex but i have the following output:
"only this to capture"
And is it also possible to only capture anything between second [] like to in this case.
A bit unclear exactly what you're after, but the following regex captures the word to in your example.
\[.*?\].*?\[(.*?)\].*?\[.*?\]
Please see the following regexr for explanation.

Regex to Match Multiple Words In Different Order [duplicate]

This question already has answers here:
What is a word boundary in regex?
(13 answers)
Closed 4 years ago.
string sample: "universal studios japan"
How do i make so that it matches with "japan universal studios"
AND also with "japan univer"
Right now I'm using the following to regex :
^(?=.*\bjapan\b)(?=.*\buniversal\b)(?=.*\bstudios\b)
which works but
^(?=.*\bjapan\b)(?=.*\buniver\b)
does not work. It has to be a complete match for the second word..
^(?=.*\bjapan\b)(?=.*\buniversal\b) would work..
What changes do i need to make?
^(?=.*\bjapan\b)(?=.*\buniver(?:sal)?\b)
You can make sal optional.
See demo.
https://regex101.com/r/wDUC7j/1

scala regex match up to a specific string but whole string if the specific word doesn't exist [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I have a question about scala regex
The thing I need to do is given a string, I need to find a sub-string up to a specific word given. For example, my regular expression looks like following
val x= "(?s)^(.*)(?=(foo|bar)".r
Then given a string, I need to find the longest sub-string until before foo or bar. This works perfectly but I would like to get the whole string if the string does not contain foo or bar at all.
Right now if I do
x.findAllIn("hello nice to meet you").toArray
it gives me an empty string but I would like to get
"hello nice to meet you" when I do that.
Does anyone have an idea how to implement that?
You can add an end-of-string assertion to the alternative:
(?s)^(.*?)(?=(foo|bar|$))
Demo