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

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.

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.

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

Regular Expressions AND [duplicate]

This question already has answers here:
Regular Expressions: Is there an AND operator?
(14 answers)
Closed 6 years ago.
I am really not good with regular expressions and I come here for some assistance :). I am trying to combine regular expressions with something like AND. For example if we have a text file with:
abc1-xyz
abc1-ertxyz
abc1xyz
postxyz
abc1
I would like to match everything that starts with "abc1" AND also contains the letters "xyz" somewhere.
I know that I can start with:
/^abc1/
but I am not sure how to combine so it can also match to contain "xyz".
Thank you for your assistance in advance.
You should tell us with which language you are coding, regex engines are not always the same.
There is another ambiguous point : Do you need your string to CONTAIN xyz or to END WITH?
Considering you are coding on Javascript..
If you want it to contain xyz, try :
/^abc1.*xyz/
If you want it to end with xyz, try :
/^abc1.*xyz$/