Regex to Match Multiple Words In Different Order [duplicate] - regex

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

Related

regex only letter and - allowed [duplicate]

This question already has answers here:
Regular expression for alpahbet,underscore,hyphen,apostrophe only
(5 answers)
Closed 2 years ago.
I want to make a pattern for input.
I have this [A-Za-z]|-, but if I type dsadsa$ this, the special characters still allowed and I need only
big and small letters, and - .
You can escape - character:
[A-Za-z\-]
Your expression should be like following:
([A-Za-z\-]+)
([\w-]+)

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.

build a string that excludes text containing two specific words [duplicate]

This question already has answers here:
Regular expression to match strings that do NOT contain all specified elements
(2 answers)
Closed 2 years ago.
I am trying to find the inverse of the following string but with no success:
(?i)(?s)^(?=.*?word1)(?=.*?word2)
I built this but it is for one word only and it does not even function properly, considering that if I test it with regex101 I got one match while I should get no match:
(?i)(?s)(?!.*?word2)^.*$
Please see the following link: https://regex101.com/r/qS7yN9/72
Hope you guys can help to build the right string.
You can effectively negate the result by wrapping a negative look-ahead assertion around the whole thing:
(?i)(?s)^(?!(?=.*?word1)(?=.*?word2))

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.