Regex not working correctly all the time, why? - regex

Regex just working in some cases, other not working.
https://regex101.com/r/p5u3N6/1
I expected regex match only groups of two "{ } { }" without nothing between { }

I'm guessing that we wish to only capture, three of our inputs listed in the demo using an expression similar to:
(\{.*?\}(.+?){.*?\})
Demo 1
or
(\{(.+?)\}(.+?){(.+?)\})
Demo 2

The .*? in the first part of your pattern is passing through the unexpected parts of your input until it finds because . accepts all of those characters. Simply making the quantifier lazy with ? isn't enough-- it will still proceed until it finds a match.
\{[^}]*?\}\s\{[^}]*?\}
https://regex101.com/r/p5u3N6/5

Not sure I understood your requirements, I suppose you only want pairs of {}{} to match, and allow nothing more than one space between these two. You can try this \{([^\{]+)\}\ \{([^\}]+)\}.

Related

Regex to match path containing one of two strings

RegEx to match one of two strings in the third segment, ie in pseudo code:
/content/au/(boomer or millenial)/...
Example matches
/content/au/boomer
/content/au/boomer/male/31
/content/au/millenial/female/29/M
/content/au/millenial/male/18/UM
Example non-matches
/content/au
/content/nz/millenial/male/18/UM
/content/au/genz/male
I've tried this, but to no avail:
^/content/au/(?![^/]*/(?:millenial|boomer))([^/]*)
Don't use a look ahead; just use the plain alternation millenial|boomer then a word-boundary:
^/content/au/(?:millenial|boomer)\b(?:/.*)?
See live demo.
You should probably spell millennial correctly too (two "n"s, not one).
What's with the negative lookahead? This is a simple, if not trivial, positive match.
^/content/au/(?:millenial|boomer)(?:/|$)
The final group says the match needs to be followed by a slash or nothing, so as to exclude paths which begin with one of the alternatives, but contain additional text.
You can use the following regex DEMO
content/au/(?:boomer|millenial)

Regex to remove a whole phrase from the match

I am trying to remove a whole phrase from my regex(PCRE) matches
if given the following strings
test:test2:test3:test4:test5:1.0.department
test:test2:test3:test4:test5:1.0.foo.0.bar
user.0.display
"test:test2:test3:test4:test5:1.0".division
I want to write regex that will return:
.department
.foo.0.bar
user.0.display
.division
Now I thought a good way to do this would be to match everything and then remove test:test2:test3:test4:test5:1.0 and "test:test2:test3:test4:test5:1.0" but I am struggling to do this
I tried the following
\b(?!(test:test2:test3:test4:test5:1\.0)|("test:test2:test3:test4:test5:1\.0"))\b.*
but this seems to just remove the first tests from each and thats all. Could anyone help on where I am going wrong or a better approach maybe?
I suggest searching for the following pattern:
"?test:test2:test3:test4:test5:1\.0"?
and replacing with an empty string. See the regex demo and the regex graph:
The quotation marks on both ends are made optional with a ? (1 or 0 times) quantifier.

Regular expression to find a same consecutive words

I'm a newbie to regular expressions and i have a problem in identifying the same consecutive words using regular expression. below is the scenario.
Here is the data :
;af;aj;am;an;ao;ap12;aq123;ar;as;ad;af1223;
and my current regular expression is (;[a-z][a-z];) and it only matches the below sets ;af; , ;am; , ;ao; , ;ar; , ;ad; but my expectation is to match all these sets. ;af;aj;am;an;ao; & ;ar;as;ad;.
Could guys please guide me how to match these patterns?
It seems like your trying to extract the substrings which are in this ;[a-z][a-z]; format. If yes, then you could simply put your regex inside a lookahead to do a overlapping match.
(?=(;[a-z][a-z];))
DEMO
(;[a-z][a-z](?=;))
Try this.This returns the group you are looking for though its not clear how they are same.
The reason why urs was not working wass due to that fact (;[a-z][a-z];) doesnt leave a ; for the next element to start with.So it is not able to match as there is no ; in front of it.A lookahead assertion doesnt cosume ; thereby enabling all matches.
See demo.
http://regex101.com/r/tF4jD3/4

Regex for \begin{?} and \end{?}

I need to match from a a string \begin{?} and \end{?} where ? is any number of alphanumerical or * characters so it must match for example \begin{align} and \end{align*}.
I tried to do it but I'm not sure what's wrong
^\\begin{[^}]*}$
Start with \begin{, following anything that's not } multiple times and close with }.
The same thing is with the \end{?} but I would like it do it inside single regex if possible.
I think below regex is what you need.
\\(begin|end){[a-zA-Z0-9*]+}
Your regex:
\\(begin|end){.*?}
the .* will grab anything between the { }, and the ? means will stop when the first } comes.
{} are special characters used for expressing repetitions so you need to escape those as well.
^\\begin\{[^}]*\}$

How to Match The Inner Possible Result With Regular Expressions

I have a regular expression to match anything between { and } in my string.
"/{.*}/"
Couldn't be simpler. The problem arises when I have a single line with multiple matches. So if I have a line like this:
this is my {string}, it doesn't {work} correctly
The regex will match
{string}, it doesn't {work}
rather than
{string}
How do I get it to just match the first result?
Question-mark means "non-greedy"
"/{.*?}/"
Use a character class that includes everything except a right bracket:
/{[^}]+}/
this will work with single nested braces with only a depth of one: {(({.*?})*|.*?)*}
I'm not sure how to get infinite depth or if it's even possible with regex
Default behaviour is greedy matching, i.e. first { to last }. Use lazy matching by the ? after your *.,
/{.*?}/
or even rather than * use "not a }"
/{[^}]*}/