Regex with exceptions to certain keywords - regex

I am trying to create regex expression (to use with Stylish extension for Firefox) that will render true for every string that has youtube.com, however will render false if string contains /user/ or /channel/.
Example
True
https://www.youtube.com/feed/trending
False
https://www.youtube.com/user/whateva/featured
https://www.youtube.com/channel/lfjafjsldsf
I don't even know where to start, tried to learn regex several times, but its beyond my understanding how it works.

I believe this should do it:
^.*youtube\.com((?!\/user\/|\/channel\/).)*$
See live demo
Proposed solution is using negative lookahead. For more information on that see:
Regex tutorial on lookarounds
Regex lookahead, lookbehind and atomic groups

Related

REGEX number not in a list failing with a long list

I have a list of the following numbers and want a Regular expression that matches when a number is not in the list.
0,1,2,3,4,9,11,12,13,14,15,16,18,19,250
I have written the following REGEX statement.
^(?!.*(0|1|2|3|4|9|11|12|13|14|15|16|18|19|250)).*$
The problem is that it correctly gives a match for 5,6,7,8 etc but not for 17 or 251 for example.
I have been testing this on the online REGEX simulators.
This should resolve your issue..
^(?!\D*(0|1|2|3|4|9|11|12|13|14|15|16|18|19|250)\b).*$
In your earlier regex you were basically saying eliminate all numbers that start with 0/1/2/3/4/9!
So your original regex would actually match 54/623/71/88 but not the others. Also the 11-19 and 250 in the list were rendered useless.
Although as others have I would also recommend you to not use regex for this, as I believe it is an overkill and a maintenance nightmare!
Also an extra note "Variable length look arounds are very inefficient too" vs regular checks.
I would do \b\d+\b to get each number in the string and check if they are in your list. It would be way faster.
You can use the discard technique by matching what you do not want and capturing what you really want.
You can use a regex like this:
\b(?:[0-49]|1[1-689]|250)\b|(\d+)
Here you can check a working demo where in blue you have the matches (what you don't want) and in green the content you want. Then you have to grab the content from the capturing group
Working demo
Not sure what regex engine you are using, but here I created a sample using java:
https://ideone.com/B7kLe0

Regular expression not working in google analytics

Im trying to build a regular expression to capture URLs which contain a certain parameter 7136D38A-AA70-434E-A705-0F5C6D072A3B
Ive set up a simple regex to capture a URL with anything before and anything after this parameter (just just all URLs which contain this parameter). Ive tested this on an online checker: http://scriptular.com/ and seems to work fine. However google analytics is saying this is invalid when i try to use it. Any idea what is causing this?
Url will be in the format
/home/index?x=23908123890123&y=kjdfhjhsfd&z=7136D38A-AA70-434E-A705-0F5C6D072A3B&p=kljdaslkjasd
so i just want to capture URLs that contain that specific "z" parameter.
regex
^.+(?=7136D38A-AA70-434E-A705-0F5C6D072A3B).+$
You just need
^.+=7136D38A-AA70-434E-A705-0F5C6D072A3B.+$
Or (a bit safer):
^.+=7136D38A-AA70-434E-A705-0F5C6D072A3B($|&.+$)
And I think you can even use
=7136D38A-AA70-434E-A705-0F5C6D072A3B($|&)
See demo
Your regex is invalid because GA regex flavor does not support look-arounds (and you have a (?=...) positive look-ahead in yours).
Here is a good GA regex cheatsheet.
To match /home/index?x=23908123890123&y=kjdfhjhsfd&z=7136D38A-AA70-434E-A705-0F5C6D072A3B&p=kljdaslkjasd you can use:
\S*7136D38A-AA70-434E-A705-0F5C6D072A3B\S*

How to write a twitter regex expression?

I need a twitter expression:
twitter.com\/[\w\/\.=-]*
This one works fine, except it does capture generic twitter widgets.js library
I would like to capture ALL twitter.com EXCEPT those who cantain "widgets.js" (using regex)
ex:
twitter.com/myaccount: GOOD
twitter.com/JohnDOes: GOOD
twitter.com/widgets.js: BAD
Regards
You can do this with a negative lookahead:
twitter.com\/(?!widgets\.js)[\w\/\.=-]*
Depending on whether you are matching part of a larger text or individual strings, you may want to add some anchors:
^twitter.com\/(?!widgets\.js$)[\w\/\.=-]*$

RegEx search and replace in Eclipse over multiple lines with start and middle and end

I am struggling to come up with a good regex search and replace for the following case.
I am doing a migration from RichFaces 3 to RichFaces 4 and so far I was able to do a lot of changes with regex.
I've got something like this:
<a:ajax execute="#this"
rendered="whatever" action="#{bean.method}
someotherstuff="whatever"
/>
What I want to do is to replace the action= with listener= in the above but without changing anything else and I only want to do it within the a:ajax tag. The order and occurrence of the attributes can vary.
So I basically need a search and replace like this:
<a:ajax(SEARCH1)action="(.+?)"(SEARCH2)/>
replace with
<a:ajax$1listener="$2"$3/>
Any ideas. I think I might need lookahead but I haven't worked with that yet
Update: The accepted solution does work in Eclipse for searching however there is a known bug in Eclipse that the replace isn't working when you use look around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=109481
if you want to replace action= with listener= inside an a:ajax tag you will need lookbehind instead of lookahead. and you must note that lookbehind in java means you must define the maximum length of the lookbehind range.
Something like (?<=a:ajax[\w\W\n]{1,100})action=" with the range of wildcards between 1 until 100, you can increase it if you want by changing the {1,100}.
You can do
.replaceAll("(?<=a:ajax[\\w\\W\\n]{1,100})action=\"","listener=\"")
Note about regex lookbehind:
Java allowing finite repetition. You still cannot use the star or
plus, but you can use the question mark and the curly braces with the
max parameter specified.
JGsoft engine and the .NET framework RegEx classes, can do full regex
inside lookbehind.
Javascript not supported lookbehind.
Python can use fixed length only inside lookbehind.

Regex for skipping specified URL extensions

I have a link. Ex: http://my.domain/url.jsp
My goal is create patter that will be not allow any URLs with extension like this: .ex1, .ex2, .ex3
I was searching a long of time and find some approach, but it's really opposite that I want.
([^\s]+(\.(?i)(ex1|ex2|ex3))$)
If lookbehind is supported then this regex should work:
^\S+$(?<!\.(?:ex1|ex2|ex3)$)
Live Demo: http://www.rubular.com/r/gQDxYdDKcU
If lookbehind isn't supported (e.g. Javascript) then use this lookahead based regex:
^(?!.*?\.(?:ex1|ex2|ex3)$)\S+$
Live Demo: http://www.rubular.com/r/S0FGAETLr2