What would be the regex to match the following path (url):
Examples :
/batches/123/details
/batches/234/something
/batches/3234/otherpath
the string should start with "/batches" & should continue to have "/3213" i.e "/{number}"
Tried the /^(batches)/(\d+)+[/]?/.test('/batches/34/details') returns false
^(\/batches\/)(\d+)
This matches everything that starts with "/batches/" and has some numbers after it.
The main things that were a bit off with your attempt were:
Not escaping the / character with a backslash
^ indicates the start of the string to match but you had it before "batches" instead of "\ /batches"
Good to test the regex here: https://regex101.com/
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 recently asked a question regarding an error I have been getting using a RegExp constructor in Javascript with lookbehind assertion.
What I want to do it, to check for a number input bigger than 5 preceded by an odd number of backslash, in other words, that is not preceded by an escaped backslash
Here is an example.
\5 // match !
\\5 // no match !
\\\5 // match!
The Regex I found online is
(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)
But the problem here is that (?<!\\) causes a problem with javascript throwing an error invalid regex group.
Is there a workaround for this ?
Finally, I know that my current regex also may have an error regarding the detection of a number larger than 5, for example \55 will not match. I would appreciate your help.
thank you
JS doesn't support lookbehinds (at least not all major browsers do), hence the error. You could try:
(?:^|[^\\\n])\\(?:\\{2})*(?![0-4]\b)\d+
Or if you care about decimal numbers:
(?:^|[^\\\n])\\(?:\\{2})*(?![0-4](?:\.\d*)?\b)\d+(?:\.\d*)?
Live demo
Note: You don't need \n if you don't have multi line text.
Regex breakdown:
(?: Beginning of non-capturing group
^ Start of line
| Or
[^\\\n] Match nothing but a backslash
) End of non-capturing group
\\(?:\\{2})* Match a backslash following even number of it
(?![0-4](?:\.\d*)?\b) Following number shouldn't be less than 5 (care about decimal numbers)
\d+(?:\.\d*)? Match a number
JS code:
var str = `\\5
\\\\5
\\\\\\5
\\\\\\4
\\4.
\\\\\\6
`;
console.log(
str.match(/(?:^|[^\\\n])\\(?:\\{2})*(?![0-4](?:\.\d*)?\b)\d+(?:\.\d*)?/gm)
)
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'm trying to build a regex which should match when only one forward slash is found and false when 2 or more forward slashes are found. The capturing group is not used, olny if it matches, and the regex is executed by javascript.
/this-should-match
/this-should/not-match
I've tried a couple of regexps, including using a negative lookahead, but I can't seem to find the solution. Some patterns I've tried:
/\/(.*)(?!\/)/i
/\/(.*)[?!\/]/i
/\/(.*[?!\/])/i
Any regex genius over here knows the solution? I'm aware regex is meant to find an occurrence of a pattern, but there should be some solution for this?
Use negated character class instead of look arounds.
^\/[^/]+$
^ Anchors the regex at the start of the string.
[^/] negated character class. Matches anything other than /
$ Anchors the regex at the end of the string. Ensures that nothing follows the string that is matched by the pattern.
Regex Demo
Example
"/this-should-match".match(/^\/[^/]+$/)
=> ["/this-should-match"]
"/this-should-match/not-match".match(/^\/[^/]+$/)
=> null
Can someone please help me in defining a regular expression for an endpoint.
person/^((?!-).)*$/
This regex needs to match a number of things but mainly:
person/:id
it should NOT match
person/1234-5678-9123 (it's currently not matching this which is good)
the problem I have is that it should NOT match this but it is:
person/123456789123 (it's currently matching this but shouldn't)
To be clear, If you go to: http://regex101.com and paste in:
^((?!-).)*$
You can see that is matches 123456789123 WHICH IS WRONG
How can I change the RegEx so it doesn't match 123456789123
Cheers.
Your regex ^((?!-).)*$ is same as ^[^-]*$ that is match any charcater but not of - zero or more times.
The reason for why your regex not matches this person/1234-5678-9123 is because it has - symbol. But person/123456789123 string isn't has - symbol, so this got matched.
To match the string which has - between the numbers then you could try the below regex.
^.*?\d+-\d+.*$
OR
^(?=.*?-).+$
(?=.*?-) Positive lookahead asserts that the string must contain an - symbol.
DEMO