Hi Guys I am trying to find the expression 'Horas extras' but if is followed by 'No Aprobadas' refuse the complete expression.
For example these cases don't not have to be considered
'Horas extras no aprobadas'
'Horas extra no aprobadas'
'Hora extras no aprobada'
My regex is the following
horas?\s+extras?(?!\s+no\s+Aprobadas?)/gmi
I am
I have this test link
https://regex101.com/r/FBq6pf/1
You may "anchor" the negative lookahead with a word boundary \b.
/\bhoras?\s+extras?\b(?!\s+no\s+Aprobada)/ig
See the regex demo.
Whenever a regex engine fails to find a match, it checks all other possible paths it could take to find a valid match at the current location. It is called backtracking. When a pattern contains quantifiers that allow matching a variable number of chars, the regex engine goes back to them and retries a match from that location.
So, in your case, since s? can match 1 or 0 s chars, once the lookahead fails, the regex engine goes back to horas extra and checks if there is \s+no\s+Aprobadas pattern after extra. There is none, thus the negative lookahead returns a valid match of horas extra. See your regex debugger view:
See, the last two steps show how the lookahead pattern is not found right after a and before s (the s is not matched with \s+).
The word boundary requires that there is a non-word char or end of string after extra or extras, so there can be no match if the engine wants to backtrack to the location before s (there is no word boundary position there).
Note that there would be no such problem if you had horas?\s+extra(?!\s+no\s+Aprobadas) regex. There is no other way to match the string other than extra before the lookahead, so no word boundary would be necessary.
Related
I have a few strings:
some-text-123123#####abcdefg/
some-STRING-413123#####qwer123t/
some-STRING-413123#####456zxcv/
I would like to receive:
abcdefg
qwer123t
456zxcv
I have tried regexp:
/[^#####]*[^\/]/
But this not working...
To get whatever comes after five #s and before the last /, you can use
/#####(.*)\//
and pick up the first group.
Demo:
const regex = /#####(.*)\//;
console.log('some-text-123123#####abcdefg/'.match(regex)[1]);
console.log('some-STRING-413123#####qwer123t/'.match(regex)[1]);
console.log('some-STRING-413123#####456zxcv/'.match(regex)[1]);
assumptions:
the desired part of the string sample will always:
start after 5 #'s
end before a single /
suggestion: /(?<=#{5})\w*(?=\/)/
So (?<=#{5}) is a lookbehind assertion which will check to see if any matching string has the provided assertion immediately behind it (in this case, 5 #'s).
(?=\/) is a lookahead assertion, which will check ahead of a matching string segment to see if it matches the provided assertion (in this case, a single /).
The actual text the regex will return as a match is \w*, consisting of a character class and a quantifier. The character class \w matches any alphanumeric character ([A-Za-z0-9_]). The * quantifier matches the preceding item 0 or more times.
successful matches:
'some-text-123123#####abcdefg/'
'some-STRING-413123#####qwer123t/'
'some-STRING-413123#####456zxcv/'
I would highly recommend learning Regular Expressions in-depth, as it's a very powerful tool when fully utilised.
MDN, as with most things web-dev, is a fantastic resource for regex. Everything from my answer here can be learned on MDN's Regular expression syntax cheatsheet.
Also, an interactive tool can be very helpful when putting together a complex regular expression. Regex 101 is typically what I use, but there are many similar web-tools online that can be found from a google search.
You pattern does not work because you are using negated character classes [^
The pattern [^#####]*[^\/] can be written as [^#]*[^\/] and matches optional chars other than # and then a single char other than /
Here are some examples of other patterns that can give the same match.
At least 5 leading # chars and then matching 1+ word chars in a group and the / at the end of the string using an anchor $, or omit the anchor if that is not the case:
#####(\w+)\/$
Regex demo
If there should be a preceding character other than #
[^#]#####(\w+)\/$
(?<!#)#####(\w+)\/$
Regex demo
Matching at least 5 # chars and no # or / in between using a negated character class in this case:
#####([^#\/]+)\/
Or with lookarounds:
(?<=(?<!#)#####)[^#\/]+(?=\/)
Regex demo
I have a regex for a game that should match strings in the form of go [anything] or [cardinal direction], and capture either the [anything] or the [cardinal direction]. For example, the following would match:
go north
go foo
north
And the following would not match:
foo
go
I was able to do this using two separate regexes: /^(?:go (.+))$/ to match the first case, and /^(north|east|south|west)$/ to match the second case. I tried to combine the regexes to be /^(?:go (.+))|(north|east|south|west)$/. The regex matches all of my test cases correctly, but it doesn't correctly capture for the second case. I tried plugging the regex into RegExr and noticed that even though the first case wasn't being matched against, it was still being captured.
How can I correct this?
Try using the positive lookbehind feature to find the word "go".
(north|east|south|west|(?<=go ).+)$
Note that this solution prevents you from including ^ at the start of the regex, because the text "go" is not actually included in the group.
You have to move the closing parenthesis to the end of the pattern to have both patterns between anchors, or else you would allow a match before one of the cardinal directions and it would still capture the cardinal direction at the end of the string.
Then in the JavaScript you can check for the group 1 or group 2 value.
^(?:go (.+)|(north|east|south|west))$
^
Regex demo
Using a lookbehind assertion (if supported), you might also get a match only instead of capture groups.
In that case, you can match the rest of the line, asserting go to the left at the start of the string, or match only 1 of the cardinal directions:
(?<=^go ).+|^(?:north|east|south|west)$
Regex demo
<table((?!</table>).)*</table>
matches all my table tags. However,
<table(.(?!</table>))*</table>
does not. The second one seems to make sense if I try to write out the expression in words, but I can't make sense of the first.
What is the difference ?
For reference, I got the term "Tempered Greedy Token" from here: Tempered Greedy Token Solution
What is a Tempered Greedy Token?
The rexegg.com tempered greedy token reference is quite concise:
In (?:(?!{END}).)*, the * quantifier applies to a dot, but it is now a tempered dot. The negative lookahead (?!{END}) asserts that what follows the current position is not the string {END}. Therefore, the dot can never match the opening brace of {END}, guaranteeing that we won't jump over the {END} delimiter.
That is it: a tempered greedy token is a kind of a negated character class for a character sequence (cf. negated character class for a single character).
NOTE: The difference between a tempered greedy token and a negated character class is that the former does not really match the text other than the sequence itself, but a single character that does not start that sequence. I.e. (?:(?!abc|xyz).)+ won't match def in defabc, but will match def and bc, because a starts the forbidden abc sequence, and bc does not.
It consists of:
(?:...)* - a quantified non-capturing group (it may be a capturing group, but it makes no sense to capture each individual character) (a * can be +, it depends on whether an empty string match is expected)
(?!...) - a negative lookahead that actually imposes a restriction on the value to the right of the current location
. - (or any (usually single) character) a consuming pattern.
However, we can always further temper the token by using alternations in the negative lookahead (e.g. (?!{(?:END|START|MID)})) or by replacing the all-matching dot with a negated character class (e.g. (?:(?!START|END|MID)[^<>]) when trying to match text only inside tags).
Consuming part placement
Note there is no mentioning of a construction where a consuming part (the dot in the original tempered greedy token) is placed before the lookahead. Avinash's answer is explaining that part clearly: (.(?!</table>))* first matches any character (but a newline without a DOTALL modifier) and then checks if it is not followed with </table> resulting in a failure to match e in <table>table</table>. *The consuming part (the .) must be placed after the tempering lookahead.
When should we use a tempered greedy token?
Rexegg.com gives an idea:
When we want to match a block of text between Delimiter 1 and Delimiter 2 with no Substring 3 in-between (e.g. {START}(?:(?!{(?:MID|RESTART)}).)*?{END}
When we want to match a block of text containing a specific pattern inside without overflowing subsequent blocks (e.g. instead of lazy dot matching as in <table>.*?chair.*?</table>, we'd use something like <table>(?:(?!chair|</?table>).)*chair(?:(?!<table>).)*</table>).
When we want to match the shortest window possible between 2 strings. Lazy matching won't help when you need to get abc 2 xyz from abc 1 abc 2 xyz (see abc.*?xyz and abc(?:(?!abc).)*?xyz).
Performance Issue
Tempered greedy token is resource-consuming as a lookahead check is performed after each character matched with the consuming pattern. Unrolling the loop technique can significantly increase tempered greedy token performance.
Say, we want to match abc 2 xyz in abc 1 abc 2 xyz 3 xyz. Instead of checking each character between abc and xyz with abc(?:(?!abc|xyz).)*xyz, we can skip all characters that are not a or x with [^ax]*, and then match all a that are not followed with bc (with a(?!bc)) and all x that are not followed with yz (with x(?!yz)): abc[^ax]*(?:a(?!bc)[^ax]*|x(?!yz)[^ax]*)*xyz.
((?!</table>).)* would check for that particular character going to be matched must not be a starting character in the string </table>. If yes, then it only matches that particular character. * repeats the same zero or more times.
(.(?!</table>))* matches any character only if it's not followed by </table>, zero or more times. So this would match all the characters inside the table tag except the last character, since the last character is followed by </table>. And the following pattern </table> asserts that there must be a closing table tag at the end of the match. This makes the match to fail.
See here.
A tempered greedy token really just means:
"match, but only up to a point"
How you do it:
you put the token you don't want to match as a negative lookahead
(?!notAllowedToMatch) in front of a dot . (match any one
thing), then you repeat that whole thing with a star *:
((?!notAllowedToMatch).)*
How it works:
"look, and eat one" over and over, moving one character at time from left to right through the input string, until the disallowed sequence (or end of string) is seen, at which point the match stops.
Wiktor's more detailed answer is nice, but I just thought a simpler explanation was in order.
I am using the following regex:
https://(dev-|stag-|)(assets|images).server.io/v[\d]/file/(.*?)/(?!(download$))
Url 1: https://assets.server.io/v3/file/blt123e25b85f95497/download.jpg
Url 2: https://images.server.io/v3/file/blt123e25b85f95497/download
Url 3: https://images.server.io/v3/file/blt123e25b85f95497/random.jpg
The intention is to match Url 1 & 3 completely, but not Url 2, but it doesn't seem to work.
By checking the following answers:
Javascript regex negative look-behind,
Regex: match everything but,
I believe a negative lookbehind would work, but am unable to figure out what the regex for that would be.
Any help with it would be greatly appreciated!
The (?!(download$)) part by itself isn't doing the right thing here since it fails the match if there is download and end of string immediately to the right of the last / matched. You need to actually match the last subpart with a consuming pattern to actually match the filename.
You may use
/https:\/\/(dev-|stag-)?(assets|images)\.server\.io\/v\d\/file\/(.*?)\/(?!download$)[^\/]+$/
^^^^^^^^^^^^^
See the regex demo. If you need to match the whole string, add ^ anchor at the start of the pattern. s may be also made optional with ? after it.
Details
https:\/\/ - a https:// substring
(dev-|stag-)? - an optional dev- or stag- substring
(assets|images) - either assets or images substring
\.server\.io\/v - a .server.io/v substring
\d - any digit
\/file\/ - a /file/ substring
(.*?) - any 0+ chars other than line break chars, as few as possible
\/ - a /
(?!download$) - there must not be a download substring followed with the end of string position immediately to the right of the current location
[^\/]+ - 1 or more chars other than /, as many as possible
$ - end of string.
Note that [\d] is less readable than \d, and you need to escape . symbols in the pattern if you want to match literal dot chars.
I have this string:
this is a test
at the end of this string I have a space and the new line.
I want to extract (for counting) all space group in the string witout the last space.
With my simple regex
/\s+/g
I obtain these groups:
this(1)is(2)a(3)test(4)
I want to exclude from group the forth space because i want to get only 3 groups if the string end with space.
What is the correct regexp?
Depending on the regex flavor, you can use two approaches.
If atomic groups/possessive quantifiers are not supported, use a lookahead solution like this:
(?:\s(?!\s*$))+
See the regex demo
The main point is that we only match a whitespace that is not followed with 0+ other whitespace symbols followed with an end of string (the check if performed with the (?!\s*$) lookahead).
Else, use
\s++(?!$)
See another demo. An equivalent expression with an atomic groups is (?>\s+)(?!$).
Here, we check for the end of string position ONLY after grabbing all whitespaces without backtracking into the \s++ pattern (so, if after the last space there is an end of string, the whole match is failed).
Also, it is possible to emulate an atomic group in JavaScript with the help of capturing inside the positive lookahead and then using a backreference like
(?=(\s+))\1(?!$)
However, this pattern is costly in terms of performance.