Regex expression to exclude both prefix and suffix - regex

I'm trying to build an expression which will match all text EXCLUDING text with prefix 'abc' AND suffix 'def' (text which only has the prefix OR the suffix is ok).
I've tried the following:
^(?!([a][b][c]])).*(?!([d][e][f])$), but it doesn't match text which only has one of the criterias (i.e. abc.xxx fails, as well as xxx.pdf, though they should pass)
I understand the answer is related to 'look behind' but i'm still not quite sure how to achieve this behavior
I've also tried the following:
^(?<!([a][b][c])).*(?!([d][e][f])$), but again, with no luck

^((abc.*\.(?!def))|((?!abc).*\.def))$
I think there can be a simpler solution, but this one will work as you wanted it.
[a][b][c] can be simplified to abc, the same goes for def.
The first part of the pattern matches abc.*\. without def at the end.
The second part matches .*\.def without the prefix abc.
Here is a visual representation of the pattern:
Debuggex Demo

Keep it simple and combine it into a single lookahead to check both conditions:
^(?!abc.*def$).*

Related

How can I write a regex that will match a nested [quote] BB tag?

As part of a forum that uses BBCode to store posts, I'm trying to write a way to detect mentions and quotes, in order to notify the users.
I have it working for all cases except nested quotes.
This is my regex so far (Python 2.7):
regex = r'\[url=.*?\/users\/(.*?)\/\]#.*?\[\/url\]|\[quote="(.*?)"\].*?\[\/quote\]'
These are my test cases:
# This works fine, I get the `user1` group.
Hello [url=/users/user1/]#Foo Bar[/url]
# This works fine, I get the `user2` and `user3` groups.
[quote="user2"]Test message[/quote] OK [quote="user3"]Test message[/quote]
# This doesn't work as I'd l ike. I only get the `user4` group, but not `user5`.
[quote="user4"][quote="user5"]Test message[/quote][/quote]
How can I modify the regular expression to match also the third test with the nested [quote] block?
Here's a link to regex101 for your convenience: https://regex101.com/r/Ov5SI1/1
Thank you!
A minor change in the original regex will solve your problem. Here is the original regex:
\[url=.*?\/users\/(.*?)\/\]#.*?\[\/url\]|\[quote="(.*?)"\].*?\[\/quote\]
Error
Consider the input string:
[quote="user4"][quote="user5"]Test message[/quote][/quote]
The last alternation tries to match it and it does succeed. However, the first match is
[quote="user4"][quote="user5"]Test message[/quote]
Now the next match starts after the [/quote]. It will not start anywhere before since all the previous text is already part of a successful match.
Correction
Solution 1:
Changing this part .*?\[\/quote\] in the original regex to a look ahead will result in successful match of both the user4 and user5.
\[quote=\"(.*?)\"\](?=.*?\[\/quote\])
final regex: \[url=.*?\/users\/(.*?)\/\]#.*?\[\/url\]|\[quote=\"(.*?)\"\](?=.*?\[\/quote\])
Solution 2:
Focusing on just the right part of the alternation - \[quote="(.*?)"\].*?\[\/quote\]
Here only \[quote="(.*?)"\] this is necessary if you want to find any patter of the form [quote="..."]. The remaining portion is unnecessary.
Here is the final regex:
\[url=.*?\/users\/(.*?)\/\]#.*?\[\/url\]|\[quote=\"(.*?)\"\]
Please do remember that the regex must be applied globally to find all the matches.

Regex capture into group everything from string except part of string

I'm trying to create a regex, which will capture everything from a string, except for specific parts of the string. The he best place to start seems to be using groups.
For example, I want to capture everything except for "production" and "public" from a string.
Sample input:
california-public-local-card-production
production-nevada-public
Would give output
california-local-card
nevada
On https://regex101.com/ I can extract the strings I don't want with
(production|public)\g
But how to capture the things I want instead?
The following will kind of get me the word from between production and public, but not anything before or after https://regex101.com/r/f5xLLr/2 :
(production|public)-?(\w*)\g
Flipping it and going for \s\S actually gives me what I need in two separate subgroups (group2 in both matches) https://regex101.com/r/ItlXk5/1 :
(([\s\S]*?)(production|public))\g
But how to combine the results? Ideally I would like to extract them as a separate named group , this is where I've gotten to https://regex101.com/r/scWxh5/1 :
(([\s\S]*?)(production|public))(?P<app>\2)\g
But this breaks the group2 matchings and gets me empty strings. What else should I try?
Edit: This question boils down to this: How to merge regex group matches?
Which seems to be impossible to solve in regex.
A regexp match is always a continuous range of the sample string. Thus, the anwswer is "No, you cannot write a regexp which matches a series of concatenated substrings as described in the question".
But, this popular kind of task is being solved very easily by replacing unnecessary words by empty strings. Like
s/-production|production-|-public|public-//g
(Or an equivalent in a language you're using)
Note. Provided that \b is supported, it would be more correct to spell it as
s/-production\b|\bproduction-|-public\b|\bpublic-//g
(to avoid matching words like 'subproduction' or 'publication')
Your regex is nearly there:
([\s\S]*?)(?>production|public)
But this results in multiple matches
Match 1
Full match 0-17 `california-public`
Group 1. 0-11 `california-`
Match 2
Full match 17-39 `-local-card-production`
Group 1. 17-29 `-local-card-`
So You have to match multiple times to retrieve the result.

regex optional part in prefix, but do not include it in matches if it present

Problem is easier to be seen in code then described I got following regex
(?<=First(Second)?)\w{5}
and following sample data
FirstSecondText1
FirstText2
I only want matches Text1 & Text2 , I get 3 though, Secon is added, and I don't want that.
Played around, cant seem to get it to work.
You need an additional negative lookahead:
(?<=First(Second)?)(?!Second)\w{5}
If you want to avoid using Second twice, you could do it without lookaround and take the result of the first capturing group:
First(?:Second)?(\w{5})
You can try this regex (?<=First(Second)?)\w{5}$. All you have to do is to add a $ in the end so that the regex would not match the text Secon. You can use this as long as you are sure of the pattern that comes at the end of the input text. In this case it is \w{5}$

How to match a string that does not end in a certain substring?

how can I write regular expression that dose not contain some string at the end.
in my project,all classes that their names dont end with some string such as "controller" and "map" should inherit from a base class. how can I do this using regular expression ?
but using both
public*.class[a-zA-Z]*(?<!controller|map)$
public*.class*.(?<!controller)$
there isnt any match case!!!
Do a search for all filenames matching this:
(?<!controller|map|anythingelse)$
(Remove the |anythingelse if no other keywords, or append other keywords similarly.)
If you can't use negative lookbehinds (the (?<!..) bit), do a search for filenames that do not match this:
(?:controller|map)$
And if that still doesn't work (might not in some IDEs), remove the ?: part and it probably will - that just makes it a non-capturing group, but the difference here is fairly insignificant.
If you're using something where the full string must match, then you can just prefix either of the above with ^.* to do that.
Update:
In response to this:
but using both
public*.class[a-zA-Z]*(?<!controller|map)$
public*.class*.(?<!controller)$
there isnt any match case!!!
Not quite sure what you're attempting with the public/class stuff there, so try this:
public.*class.*(?<!controller|map)$`
The . is a regex char that means "anything except newline", and the * means zero or more times.
If this isn't what you're after, edit the question with more details.
Depending on your regex implementation, you might be able to use a lookbehind for this task. This would look like
(?<!SomeText)$
This matches any lines NOT having "SomeText" at their end. If you cannot use that, the expression
^(?!.*SomeText$).*$
matches any non-empty lines not ending with "SomeText" as well.
You could write a regex that contains two groups, one consists of one or more characters before controller or map, the other contains controller or map and is optional.
^(.+)(controller|map)?$
With that you may match your string and if there is a group() method in the regex API you use, if group(2) is empty, the string does not contain controller or map.
Check if the name does not match [a-zA-Z]*controller or [a-zA-Z]*map.
finally I did it in this way
public.*class.*[^(controller|map|spec)]$
it worked

Regular Expression - Want two matches get only one

I'm working wih a regular expression and have some lines in javascript. My expression should deliver two matches but recognizes only one and I don't know whats the problem.
The Lines in javascript look like this:
if(mode==1) var adresse = "?APPNAME=CampusNet&PRGNAME=ACTION&ARGUMENTS=-A7uh6sBXerQwOCd8VxEMp6x0STE.YaNZDsBnBOto8YWsmwbh7FmWgYGPUHysiL9u0.jUsPVdYQAlvwCsiktBzUaCohVBnkyistIjCR77awL5xoM3WTHYox0AQs65SoHAhMXDJVr7="; else var adresse = "?APPNAME=CampusNet&PRGNAME=ACTION&ARGUMENTS=-AHMqmg-jXIDdylCjFLuixe..udPC2hjn6Kiioq7O41HsnnaP6ylFkQLhaUkaWKINEj4l2JqL2eBSzOpmG.b5Av2AvvUxEinUhMBTt5awdgAL4SkBEgYXGejTGUxcgPE-MfiQjefc=";
My expression looks like this:
(?<Popup>(popUp\(')|(adresse...")).*\?((?<Parameters>APPNAME=CampusNet[^>"']*["']))
I want to have two matches with APPNAME...... as Parameters.
[UPDATE] Like Tim Pietzcker wrote i used the greedy version and should have used the lazy version. while he wrote that i solved it myself by using .? instead of . in the middle so the expression looks like this:
(?<Popup>(popUp\(')|(adresse...")).*?\\?((?<Parameters>APPNAME=CampusNet[^>"']*["']))
That worked. Thanks to Tim Pietzcker
Your regex matches too much - from the very first adresse until the very last " because it uses a greedy quantifier .*.
If you make that quantifier lazy, i. e.
(?<Popup>(popUp\(')|(adresse...")).*?\?((?<Parameters>APPNAME=CampusNet[^>"']*["']))
you get two matches.
Alternatively, if your data allows this, use a different quantifier that only matches non-space characters. This will match faster (but will fail of course if the text you're trying to match could possibly contain spaces):
(?<Popup>(popUp\(')|(adresse..."))\S*\?((?<Parameters>APPNAME=CampusNet[^>"']*["']))
Usually you must apply the regex with the "global" flag to find all matches. I can't really say more until I see the complete code sample you are working with.