This question already has answers here:
RegEx to get section of URL after string in Javascript
(2 answers)
Closed 1 year ago.
I’m trying to extract a string between 2 slashes in a url for example:
HTTPS://www.google.com/blabla/ab /what2/what3/lalaalala
I want to extract the ab which is between 2 slashes in the middle. How can I extract it? It always comes after blabla if it helps.
I tried:
([^\/]+$)
You want to use a positive lookbehind for that: (?<=...)
It checks if something exists before your match without capturing it in the result.
Here is the regex:
(?<=\/blabla\/)([^\/]+)
Now you only get ab as a result.
https://regex101.com/r/4gEsfP/1
Note: It's "positive" because it checks whether it is present, and "lookbehind" because it looks behind the ([^\/]+) pattern. There are also negative lookbehinds and positive/negative lookaheads.
Some good resource about it: https://javascript.info/regexp-lookahead-lookbehind
Related
This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 2 years ago.
Hey I have a list of files
B123245.xml
B123245-ext.xml
1234W01.xml
1234W01-ext.xml
Now I need a regular expression filter only the files without -ext in the name.
I tried already this ^.+(?!-ext)\.xml$
but it is not working.
What am I doing wrong?
Not sure about your exact needs, but if you want to exclude those file where "-ext" is right before the xml extension I think you could use:
^.+(?<!-ext)\.xml$
See the demo
^ - Start string anchor.
.+ - 1+ character apart from newline.
(?<!-ext) - A negative lookbehind to assert position isn't preceded by "-ext".
\.xml - Match a literal dot and "xml".
$ - End string anchor.
With the help of user 'The fourth bird' I found out the correct structure.
Here is the correct result
^(?!.*-ext).+\.xml$
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 2 years ago.
I would like to come out with a regex expression that negate the matched results of regex expression: .google.*search. And, is it possible to achieve it with regex from the regex expression I am trying to negate?
Test data
[1] https://www.google.com/search?newwindow=1&sxsrf=ALeKk02MzEfbUp3jO4Np
[2] https://github.com/redis/redis-rb
[3] https://web.whatsapp.com/
Expected result
Row 2, 3 match the regex pattern and are part of the results.
the following regex does the trick
^(?!.+google.*search)
basically matching the beginning of the line then negating (?!) (negative lookahead) your regex.
You may use a negative lookahead here:
https?:\/\/(?!.*\.google\..*search).*
Demo
The "secret sauce" here is (?!.*\.google\..*search), which asserts that .google. followed by search does not occur anywhere within the URL to the right of the https:// portion.
This question already has answers here:
Regex Last occurrence?
(7 answers)
Closed 3 years ago.
I have the following RegEx syntax that will match the first date found.
([0-9]+)/([0-9]+)/([0-9]+)
However, I would like to start from the end of the content and search backwards. In other words, in the below example, my syntax will always match the first date, but I want it to match the last instead.
Some Text here
01/02/15
Some additional
text here.
10/04/14
Ending text
here
I believe this is possible by using a negative lookahead, but all my attempts failed at this because I don't understand RegEx enough. Help would be appreciated.
Note: my application uses RegEx PCRP.
You could make the dot match a newline using for example an inline modifier (?s) and match until the end of the string.
Then make use of backtracking until the last occurrence of the date like pattern and precede the first digit with a word boundary.
Use \K to forget what was matched and match the date like pattern.
^(?s).*\b\K[0-9]+/[0-9]+/[0-9]+
Regex demo
Note that the pattern is a very broad match and does not validate a date itself.
This question already has answers here:
Regex.Match whole words
(4 answers)
Closed 3 years ago.
I have the following regex:
^USD|AUD|BRL|GBP|CAD|CNY|DKK|AED|EUR|HKD|INR|MYR|MXN|NZD|PHP|SGD|THB|ARS|COP|CLP|PEN|VEF$
When using this example string: 16ccf52b144~~refCode-3-d5779a89-d437-448a-bf53-efad2cdd66f6~20191020T16:00~20191026T16:00~USD~305.81~~~~**8294A2B49CD60ABE4FC7081F05CD06AA17E837CCADEB0ABC57B6AC94B09882FB
I am expecting the regex to return USD, instead it is returning CAD. How can I edit the regex so that it returns USD ...Ideally regex should look at ~currencyCode~ ...instead right now it is looking at currencyCode without tilde.
The ^ and $ assertions are unnecessary in your regex since the substring you are trying to match is at neither the beginning nor the end of the string, and the fact that ^ is preceding USD means that the pattern can only match USD if it is at the beginning of the string.
Instead, group the alternations and surround them with word boundary assertions:
\b(?:USD|AUD|BRL|GBP|CAD|CNY|DKK|AED|EUR|HKD|INR|MYR|MXN|NZD|PHP|SGD|THB|ARS|COP|CLP|PEN|VEF)\b
You haven't said which language or framework you're using, so I'll assume you want a generally applicable regex.
If you know that ~ will precede and follow your currency, then you can use a zero-width assertion to find text between ~ characters like so:
(?<=~)(USD|AUD|BRL|GBP|CAD|CNY|DKK|AED|EUR|HKD|INR|MYR|MXN|NZD|PHP|SGD|THB|ARS|COP|CLP|PEN|VEF)(?=~)
This will match the USD in 6:00~USD~305 because it's surrounded by ~, but not the CAD in 7CCADEB0 because it's not surrounded by them.
This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 4 years ago.
After countless hours of trying to get this regex to work (including looking all over StackOverflow), I thought I'd reach out for help on here as I have not been successful).
I have tried creating a regex to match everything and to not match any parameters that look like this:
text=3242ffs3F34
The data after the = sign can be random (its a mixture of numeric and string characters) and is never the same. So far I have created the following regex below, which is almost doing what I am after but it does not work.
\b(?!text=.*)\b\S+
Assistance is much appreciated!
EDIT:
I will be using the regex to match everything in a file but to filter out all parameters that look like this:
text=3242ffs3F34
Below is an example of how the config file will look like:
This is a test
test=asda
test2=22rr2
text=3242ffs3F34
test5=hello
To match everything except strings containing LAST_DOMINO_TIME= as substring you can use the expression:
(?!.*\bLAST_DOMINO_TIME=.*$)^.*$
(?! Negative lookahead.
.* Match anything.
\b Word boundary.
LAST_DOMINO_TIME= Literal substring.
.*$ Anything up to end of string.
) Close lookahead.
^.*$ Assert position beginning of line, match anything up to end of line.
You can try it here.