I have a regex:
\[[^\[\]]*\]\s*[<>|<=|>=|=|>|<]\s*'?"?\w*'?"?
this basically just parse a equation like:
[household_roster_relationships_topersona_nameadditionalpersono] = "1"
it works good, with
'=','>','<'.
but when the equation has
'<=','>=','<>'.
the parse stop at first character of
'<=','>=','<>'.
I have created a demo on regex101
How can I correct regex so it will work in this situation?
Just change your char class for an alternation:
\[[^\[\]]*\]\s*(<>|<=|>=|=|>|<)\s*'?"?\w*'?"?
^ ^
See demo
You need to replace the character class with a grouping construct.
Use
\[[^[\]]*]\s*(?:<>|[<>]=|[=><])\s*['"]?\w*['"]?
^^^ ^
See the regex demo. The (?:<>|[<>]=|[=><]) non-capturing group (only used for grouping subpatterns) matches either <>, <=, >=, =, > or <.
Note I reduced some alternative branches to make the pattern a bit more compact. Also, I think you just want to match either ' or " at the end, so, a mere ['"]? (1 or 0 ' or ") should be enough.
Also, you do not need to escape a [ inside a character class ([[] matches a single [) and you do not need to escape ] outside a character class, it matches a literal ] symbol.
You need to use () instead of [] for multiple word character class selection ...see updated regex
Related
After a long, unsuccessful search I am starting to wonder if what I am looking for is possible, I would like a regular expression which requires that each letter chosen is mandatory but only once and in any order.
Example : ^[abc]{3}$
The result I expect would be that it matches only that :
abc, bac, cba, acb
While I get :
acc, abb, cca, aab
Do you see where I am going with this?
You may use a regex like this with a negative lookahead of the matched character in a back-reference:
^(?:([abc])(?!.*\1)){3}$
RegEx Demo
Here is another way.
^(?!.*([abc]).*\1)[abc]{3}$
Demo
The negative lookahead
(?!.*([abc]).*\1)
asserts that no character is repeated and
[abc]{3}
together with the two anchors asserts that the string has a length of three and is composed of the characters in the character class.
I need a regular expression that matches the following conditions:
logger(textthatdoesnotcontain|)
Example:
logger(sample log) // Match
logger(sample log | test) // Not Match
I have tried below regex, but not working:
logger(?!*\|.*)
logger\([^\|]+\) should do the trick
Or if you want to match whole lines
^logger\([^\|]+\)$
This accept any character except "|" between "logger(" and ")"
If you want your line to not match if it contains |, you can just use this regex,
^logger\([^|]*$
Demo
You don't need a negative look ahead when you want to fail the match just because of one character and can use negated character set for such use cases like this [^|]
So I have the string in the following format
[randomstring] [randomtest]
[randomstring] [texttext...
Data:
{"data}]
So the only thing in common for every line is that all text is stored inside exactly 2 square brackets per line, [text1][text2] . The problem is when the text goes on multiple lines:
[text1][text2
text3
text4]
So I'm looking for a regex to match every [][] pair, per line and came up with this:
https://regex101.com/r/vI0oF6/1
As you can see, only the first line is matched and not the second. Is there a better way ?
You have two options. Use the s modifier to match newlines with ., or simply accept newlines inside the square brackets.
With the s modifier
/(\[.+?\]+\s?\[.+?\])/gs
https://regex101.com/r/vI0oF6/5
Without the s-modifier
/(\[(?:.|\n)+?\]+\s?\[(?:.|\n)+?\])/g
https://regex101.com/r/vI0oF6/6
Note that I'm creating a non-capturing group with the (?:.|\n) syntax.
Also, notice that I used the non-greedy matching token ? inside the square brackets to make it stop matching when it first meets a square bracket instead of being greedy and also matching square brackets with the dot. Visualized, the ? after a quantifier (* or +) does this:
Without ?, .+ is greedy and matches until the last met ]
# Simple example: \[.+\]
[foo][bar]
^--------^
With ?, .+ is non-greedy and matches only until the first met ]
# Simple example: \[.+?\]
[foo][bar]
^---^
Use s modifier to include new lines (Dot matches new line characters).
https://regex101.com/r/vI0oF6/2
Try with this expression (\[[^\]]+\])
http://regexr.com/3duup
I guess this is the one you are looking for (\[[^\]]+\]).
It matches a [ followed by one or more characters other than ], followed by a ]. If you want to match ones without anything inside the brackets as well, use * instead of +.
Note: My understanding is that you need to match [text1] and [text2] from first line, [text1] and
[text2
text3
text4]
from lines 3 to 5 when input is
[text1] [text2]
[text1][text2
text3
text4]
/test-test-test/test.aspx
Hi there,
I am having a bit difficult to retrieve the first bit out from the the above URL.
test-test-test
I tried this /[\w+|-]/g but it match the last test.aspx as well.
Please help out.
Thanks
One way of doing it is using the Dom Parser as stated here: https://stackoverflow.com/a/13465791/970247.
Then you could access to the segments of the url using for example: myURL.segments; // = Array = ['test-test-test', 'test.aspx']
You need to use a positive lookahead assertion. | inside a character class would match a literal | symbol. It won't act like an alternation operator. So i suggest you to remove that.
[\w-]+(?=\/)
(?=\/) called positive lookahead assertion which asserts that the match must be followed by an forward slash. In our case test-test-test only followed by a forward slash, so it got matched. [\w-]+ matches one or more word character or hyphen. + repeats the previous token one or more times.
Example:
> "/test-test-test/test.aspx".match(/[\w-]+(?=\/)/g)
[ 'test-test-test' ]
[\w+|-] is wrong, should be [\w-]+. "A series of characters that are either word characters or hyphens", not "a single character that is a word character, a plus, a pipe, or a hyphen".
The g flag means global match, so naturally all matches will be found instead of just the first one. So you should remove that.
> '/test-test-test/test.aspx'.match(/[\w-]+/)
< ["test-test-test"]
I would like to select everything what's not [a-z0-9 ]
So if the string is:
Hello! How are you Mr. 007?
Then the result would be: !,?,.
Use a negated character class:
[^a-z0-9]
You will also need a case-insensitive modifier, or alternatively use this: [^a-zA-Z0-9].
Note that your expected result is incorrect: you missed the space character. If you want to also not match the space character you need to include it in the character class: [^a-zA-Z0-9 ].
With ^ at the beginning of a character class, you negate that class:
str.match(/[^a-z0-9 ]/gi)
You should also add space (or \s (which is space, tab,...)) and the modifier i to make the match case insensitive.
Read more about regular expressions in JavaScript and regular expressions in general.