I need a regexp that I want to match against several different strings.
The regex should retrieve a match for this string:
http://www.domain.com/category
But not for this:
http://www.domain.com/category/sports
or
http://www.domain.com/category/sportsmanship
I have tried the following regex but it doesn't really want to work:
/categ.*?^((?!sports).)*$/g
You can use this regex:
.*/category(?!/sports).*
Working demo
You can write
/\/categ[^\/]*(?:\/(?!sports)[^\/]*)*$/
In this pattern, the negative lookahead checks after each slashes if the string "sports" doesn't follow.
Note: if you have to deal with long paths that contains the string "sports" relativly often, you can try this variant to speed up the pattern:
/\/categ(?=([^\/]*))\1(?:\/(?!sports)(?=([^\/]*))\2)*$/
I think you don't want to match the line which contains the string sports at the last. If yes then you could try the below regex,
^.*?categ(?:(?!sports).)*$
The problem with your regex is .*?^. ^ asserts that we are at the start of a line.
Related
Let's say I have the text a123456. I want a string of b123456 to match. So essentially, 'match if all characters are the same except for the first character'. Am I asking for the impossible with regex?
Use the dot (.) to match any character. So, a possible Regex would be:
/^.123456$/
If you want to use zero length assertion with regex, you can have lookbehind approach in following way :
(?<=\w)your_value$ // your_value should be text which you want to check
I think you can figure it out on your own. This ain't tough, just needs some understanding between you and Regex. Why don't you go through the following links and try to make a regex on your own.
https://www.talentcookie.com/2015/07/regular-expressions/
https://www.talentcookie.com/2015/07/lets-practice-regular-expression/
https://www.talentcookie.com/2016/01/some-useful-regular-expression-terminologies/
I'm trying to filter words which is not in the "[ ]".
Why is this not working?
[^\[][\u0000-\u024F]+[^\]]
The reason your expression is not working is that it matches all text inside brackets as well as outside.
This is the best I've been able to do:
/(?:^|])[^[]+/g
It includes the ]s in the match because look-behind is not allowed:
http://regexr.com/3c515
If look-behind were allowed, this would be the ticket:
/(?:^|(?<=]))[^[]+/g
https://regex101.com/r/lK9tS7/3
Because this will match [\u0000-\u024F]+ and 2 character which will be matches by [^\[]. If you want to your regex engine match the whole of pattern you need to use start and end anchors in your regex :
/^[^\[][\u0000-\u024F]+[^\]]$/m
But this will work if your string is contain words in each line, which is not a proper way.
As a better way you can use negative look arounds :
(?<!\[)[\u0000-\u024F]+(?!\])
I have 15 titles as follows:
fruits-and-flowers-themeA
fruits-and-flowers-themeB
fruits-and-flowers-just-test-themeA
themeAfruitsandflowers
nice-fruits-and-flowers-themeA
botanical-names-themeA
I want a regex to help me get only those titles with "themeA" in them, but it should not include "nice" and not include "just-test" or "just-tests".
I tried
^(?!.*just-test|*just-tests|nice).*?(?:themeA).*,
but I still get fruits-and-flowers-just-test-themeA in the output.
How to fix this?
Thanks
You can use this regex with negative lookahead:
^(?!.*?(?:just-tests?|nice)).*?themeA.*$
Working Demo
Option 1
You can use a single regex with lookaheads (see online demo):
^(?!.*nice?)(?!.*just-tests?).*themeA.*
The ^ asserts that the match starts at the beginning of the string (so we don't match a subset of the string
The (?!.*nice?) is a negative lookahead that asserts that at this position in the string, we cannot find any characters followed by nice
The (?!.*just-tests?) is a negative lookahead that asserts that at this position in the string, we cannot find any characters followed by just-test and an optional s
As a further tweak, you can compress the lookaheads into one using an | alternation as in anubhava's answer.
Option 2 without lookaheads (Perl, PHP/PCRE)
^(?:.*(?:nice|just-tests?).*)(*SKIP)(?!)|.*themeA.*
This one doesn't use lookaheads but just skips the unwanted titles. See demo.
Use two different regular expressions for clarity and simplicity.
Match your string against one regex that matches themeA:
/themeA/
and then check that the string does NOT match the one you don't want:
/nice|just-tests?/
Doing it in two different regexes makes it far easier to understand and maintain.
Since i'm not so good at regex how can i match some conditions in a string, StaticString_1number:1number:more than 1number.
Example:
string_3:0:12344555 - Match
string_s:0:12344555 - No match
string_3:s:12344555 - No match
string_3:0:123s4555 - No match
Thanks.
This Regex would solve your problem:
^[a-zA-Z]+_[\d]{1}:[\d]{1}:[\d]+$
You can check this link for verification: http://regexr.com?34uj5
If I understand your pattern StaticString_1number:1number:more than 1number correctly your regex to match against such strings could look like the following:
'^[a-zA-Z]+_[0-9]:[0-9]:[0-9]+$'
or if your environment support character classes:
'^\w+_\d:\d:\d+$'
This should suit your needs:
^[^_]+_\d:\d:\d+$
Demo
If the initial String can only have characters a-z then the following should work :
[a-z A-Z]+_\d:\d:\d+
this will match any number of letters up to an underscore then look for single digit before and after colon and multiple digits after second colon.
but you should really have an attempt your self. if in python you could try re-try or in javascript regexpal and try out your regex patterns there first.
This may help : ^[a-zA-Z]*_[0-9]:[0-9]:[0-9]*$
I search for a regex pattern, which shouldn't match a group but everything else.
Following regex pattern works basicly:
index\.php\?page=(?:.*)&tagID=([0-9]+)$
But the .* should not match TaggedObjects.
Thanks for any advices.
(?:.*) is unnecessary - you're not grouping anything, so .* means exactly the same. But that's not the answer to your question.
To match any string that does not contain another predefined string (say TaggedObjects), use
(?:(?!TaggedObjects).)*
In your example,
index\.php\?page=(?:(?!TaggedObjects).)*&tagID=([0-9]+)$
will match
index.php?page=blahblah&tagID=1234
and will not match
index.php?page=blahTaggedObjectsblah&tagID=1234
If you do want to allow that match and only exclude the exact string TaggedObjects, then use
index\.php\?page=(?!TaggedObjects&tagID=([0-9]+)$).*&tagID=([0-9]+)$
Try this. I think you mean you want to fail the match if the string contains an occurence of 'TaggedObjects'
index\.php\?page=(?!.*TaggedObjects).*&tagID=([0-9]+)$