Negative lookbehind in javascript - javascript

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.

Related

Extracting text from a string after 5 characters and without the last slash

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

Capture a string (from a certain point) with regex not starting with certain letters

I am in the process of writing a regex that captures everything from a certain point if the string doesn't start with certain letters.
More precisely I want to capture everything from - up until a comma, only IF this string doesn't start with pt.
en-GB should capture -GB
But if the word starts with pt I simply want to skip the capture:
pt-BR should capture nothing.
I created this regex:
-[^,]*
Which works nicely except that this also captures strings beginning with pt.
Unfortunately I can't use lookbehinds since its not supported by JS, so I tried using a negative lookahead like this:
^(?!pt).*
Problem is that this captures the entire string, and not from -. I tried replacing .* with something that starts capturing at -but I haven't been successful so far.
I am kinda new to regex so any guideance would be helpful.
To match pt- and any two letters at the start of the string or any two other letters, you may use
text.match(/^(?:pt-[a-zA-Z]{2}|[a-zA-Z]{2})/)
See the regex demo. Details:
^ - start of string
(?:pt-[a-zA-Z]{2}|[a-zA-Z]{2}) - either of the two alternatives:
pt-[a-zA-Z]{2} - pt- and any two ASCII letters
| - or
[a-zA-Z]{2} - any two ASCII letters
It looks like you need to use a .replace method for some reason. Then, you may use
text.replace(/\b(?!pt-)([A-Za-z]{2})-[a-zA-Z]{2}\b/, '$1')
See this regex demo. Details:
\b - a word boundary
(?!pt-) - no pt- allowed immediately to the right of the current location
([A-Za-z]{2}) - Group 1: any two ASCII letters
- - a hyphen
[a-zA-Z]{2} - any two ASCII letters
\b - a word boundary

Refuse complete RegEx if is follewed by other Expression JS

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.

Regex for hashtags at the very begining not works in C# but works in Javascript

guys!
I writed this kind of regex I need
^((#\w+\b(\s?|#))+)
and it works fine... But only here (in Javascript mode).
As you can see, it highlights all lines till the text with no sign of hashtags begins (I only need get them from very beginning of the text).
If I'll try something like this at http://regexstorm.net/tester it would look like this (so part I need not fully captured, ECMAScript option not helps as well)
Whats the best way to fix it for C#? And why it doesnt work like that (because at other options in regex101 everything looks good)?
The main issue is the difference of line break style between Regex101 and RegexStorm sites: the first one uses LF and the latter uses CRLF styles. So, the \s? only matching 1 or 0 whitespaces fails to find a match at RegexStorm since there are two whitespaces between the end of the first and the beginning of the second line.
You might fix it changing \s? with \s* (or at least \s{0,2} to match 0 to 2 whitespaces).
However, your regex needs improving since it is causing too much overhead for the regex engine. You may write it linearly as
^#\w+(?:\s*#\w+)*
See the RegexStorm regex demo. It matches a hashtag, followed with 0+ sequences of 0+ whitespaces and a hashtag.
Note that ^ may be redefined to match the start of a line. To avoid that, in .NET, you may use \A anchor that always matches the start of the string.
Pattern details:
^ (or \A) - start of the string
#\w+ - a # followed with 1+ word chars
(?:\s*#\w+)* - zero or more sequences of:
\s* - zero or more whitespaces
#\w+ - a hashtag pattern.

Is this regex the most efficient way of parsing my string?

First off, here are the parameters to follow in the string I allow the user to input:
If there is a slash, it has to appear at the start of the string, nowhere else, is limited to 1, is optional and must be succeeded by [a-zA-Z].
If there is a tilde, it has to appear after a space " ", nothing else, is optional and must be succeeded by [a-zA-Z]. Also, this expression is limited to 2. (ie: ~exa ~mple is passed but ~exa ~mp ~le is not passed)
The slash followed by a word is an instruction, like /get or /post.
The tilde followed by a word is a parameter like ~now or ~later.
String format:
[instruction] (optional) [query] [extra parameters] (optional)
[instruction] - Must contain / succeeded with [a-zA-Z] only
[query] - Can contain [\w\s()'-] (alphanumeric, whitespace, parentheses, apostrophe, dash)
[extra parameters] - ~ preceded by whitespace, succeeded with only [a-zA-Z]
String examples that should work:
/get D0cUm3nt ex4Mpl3' ~now
D0cUm3nt ex4Mpl3'
/post T(h)(i5 s(h)ou__ld w0rk t0-0'
String examples that shouldn't work:
//get document~now
~later
example ~now~later
Before I pass the string through the regex I trim any whitespace at the start and end of the string (before any text is seen) but I don't trim double whitespaces within the string as some queries require them.
Here is the regex I used:
^(/{0,1}[a-zA-Z])?[\w\s()'-]*((\s~[a-zA-Z]*){0,2})?$
To break it down slightly:
[instruction check] - (/{0,1}[a-zA-Z])?
[query check] - [\w\s()'-]*
[parameter check] - ((\s~[a-zA-Z]*){0,2})?
This is the first time I've actually done any serious regex away from a tutorial so I'm wondering is there anything I can change within my regex to make it more compact/efficient?
All fresh perspectives are appreciated!
Thanks.
From your regex: ^(/{0,1}[a-zA-Z])?[\w\s()'-]*((\s~[a-zA-Z]*){0,2})?$,
you can change {0,1} to ? that is a shortcut to say 0 or 1 times:
^(/?[a-zA-Z])?[\w\s()'-]*((\s~[a-zA-Z]*){0,2})?$
The last part is present 0,1 or 2 times, then the ? is superfluous:
^(/?[a-zA-Z])?[\w\s()'-]*(\s~[a-zA-Z]*){0,2}$
The first part may be simplified too, the ? just after the / is superfluous:
^(/[a-zA-Z])?[\w\s()'-]*(\s~[a-zA-Z]*){0,2}$
If you don't use the captured groups, you can change them to non-capture group: (?: ) that are more efficient
^(?:/[a-zA-Z])?[\w\s()'-]*(?:\s~[a-zA-Z]*){0,2}$
You can also use the case-insensitive modifier (?i):
^(?i)(?:/[a-z])?[\w\s()'-]*(?:\s~[a-z]*){0,2}$
Finally, as said in OP, ~ must be followed by [a-zA-Z], so change the last * by +:
^(?i)(?:/[a-z])?[\w\s()'-]*(?:\s~[a-z]+){0,2}$
This looks slightly better:
^(?:/?[a-zA-Z]*\s)?[\w\s()'-]*(?:\s~[a-zA-Z]*)*$
https://codereview.stackexchange.com/ is more the place for this kind of thing
Assuming that capture groups are useful to you:
^((?:\/|\s~)[a-z]+)?([\w\s()'-]+)(~[a-z]+)?$
Regex101 Demo
Maybe this is what you're looking for:
var regex = /^((\/)?[a-zA-Z]+)?[\w\s()'-]*((\s~)?[a-zA-Z]+){0,2}$/;

Categories

Resources