REGEX must include substring [duplicate] - regex

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.

Related

Why use a negated empty set in a Regular Expression rather than .? [duplicate]

This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I came across the following RE recently:
(?:<div class="something[^>]+?>[^]+<span class="somethingelse">([\d.])+|<div id="anotherthing[^"]+" class="[^"]*andanother)
The set [^] is valid in JavaScript but not in PCRE. My understanding of it is that it will match "anything not in the set". Since the set is empty, it must be matching anything (one or more times). So why would someone use [^]+ rather than simply .+ ? Is there some very subtle difference, perhaps to do with line endings?
. does not match line endings by default. You can enable the DOTALL option (e.g /.+/s) to make it match line endings as well.

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

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))

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