Negative look-ahead expression - regex

I'm writing a Perl script that performs different actions on hosts depending on which patterns match the FQDN. I've been struggling to find a regular expression that skips hosts that have the string 'test' in the domain name.
These host names represent the four host name types I'm dealing with:
node01.prod.com
node01.test.com
node02.dmz.prod.com
node02.dmz.test.com
The following expression matches the host name pattern I'm trying to skip:
/\w\.test/
But, none of the negative look-ahead expressions I've tried will skip the host names with 'test'. For example, this expression:
/\w\.(?!test)/
matches/passes all four host name types, including the two that contain the string 'test'.
What's really driving me crazy is that if I hard code part of the host name, the negative look-ahead expression does skip the full host name:
/node01\.(?!test)/ # only matches node01.prod.com
I'm surely missing something terribly obvious - any suggestions?

The problem is that you're putting the negative lookahead after your match, which allows it to match a partial node name even if it has the word test in it somewhere.
This expression will match any string that doesn't contain test:
(?!.*test)^.*$
Online demonstration:
http://regex101.com/r/rZ0vO2

You can use this regex:
/\w\.(?!test).+/
Your negative lookahead is correct but your regex is not really matching anything after dot.

Something like the following should do it:
/(?:\w+\.(?!test))+\w+/
Depending on how you are using the regex, you may also need some anchors to prevent this from matching after test:
/^(?:\w+\.(?!test))+\w+$/
This works by putting the negative lookahead within a repeating group, so that it is checked after each . that is matched in your regex.
For example: http://rubular.com/r/TeUYi9EIEL

Did you try this pattern?
/(?!.*\.test\.)^.+/

Related

Laravel Routes Regular Expression start with # character

I need to create a route that responses to any string starting with '#' character. Routes like following examples :
www.mywebsite.com/#john
www.mywebsite.com/#jack
www.mywebsite.com/#something
So I wrote:
Route::get('{something}','SomeController#someMethod')->where('something','/#^/');
But when I test it, I face 404 not found found page.
what is the correct regular expression for this?
Route::get('/{tag}', 'SomeController#someMethod')->where('tag', '^#.*');
This will also work:
Route::get('#{something}', 'SomeController#someMethod');
You can write this
Route::pattern('tag', '#[a-zA-Z]');
Route::get('{tag}', 'SomeController#someMethod');
This way you seperate the logic of the regex and the route and it will work as you want
Note the #^ pattern means # should be followed with the beginning of string, which is not possible, and the pattern never matches any string. The '^#' pattern asserts the position at the start of the string, and only there does it try to match #.
Also, the usual / regex delimiters should be removed from this pattern as they are treated as part of the pattern here.
So, in your case you may just swap the anchor and the # char:
Route::get('{something}','SomeController#someMethod')->where('something','^#');

How to get the queryparam vid from the url using regex

Help me with the regex, I am trying to get the vid value from the following url.
I tried with like the following but I am not sure with that:
[\&]{1}vid[\=][\d]*
Is that correct?
Use vid=(\d+) for numbers of IDs see regex
Try Your Regex on this place...
https://regex101.com/r/dX3hD4/1
The trick here is to match between two patterns of interest -
"vid="
"&"
Anything you capture between that is what you're after.
Hence use this:
"http://gorid.com/api.jsp?acs=123&vid=432&skey=asdasd-asdas-adsasd".match("vid=([^;]*)&")[1]
We're accessing the 2nd element of the match object because that contains the value.
In a JS/PHP type environment, you can match on something like this, where you just find anything alphanumeric is between vid= and the following &:
vv = str.match(/vid=(.+?)&/)[1];
HERE
If the value is always numeric, replace (.+?) with (\d+?)
The regex you wrote will not work because you are including the characters &vid= in the return value. To make sure the regex engine checks for the string &vid= but does not include it in the result you will need to use a lookbehind:
(?<=&vid=)([^&\r\n]+)
We use a positive lookbehind to find &vid= and then grab everything from that point until the next & sign or the end of the line.
For your second request, if you wish to verify that the content of vid is a valid number you need to specify that all the characters following &vid= should be digits and also include a positive lookahead that makes sure the next character after the digits is a & sign. The corresponding regular expression then becomes:
(?<=&vid=)([^\D]+)(?=&)

URL rewrite using PCRE expression - append prefix to all incoming URIs except one pattern

i am using match expression as https://([^/]*)/(.*) and replace expression as constantprefix/$2 and trying to rewrite incoming URL by adding '/constantprefix' to all URLs
for Below URLs it is working as expected:
https://hostname/incomingURI is converting to
/constantprefix/incomingURI
https://hostname/ is converting to /constantprefix/
https://hostname/login/index.aspx is converting to
/constantprefix/login/index.aspx
i am having problem for the URLs which already starting with /constantprefix, i am seeing two /constantprefix/constantprefix in the output URL which I am not looking for, is there any way we can avoid that ?
if incoming URL is https://hostname/constantprefix/login/index.aspx then output URL is becoming https://hostname/constantprefix/constantprefix/login/index.aspx
may i know how i can avoid /constantprefix/constantprefix from match expression ?
You can do it with:
https://[^/]*/(?!constantprefix(?:/|$))(.*)
using the replacement string:
constantprefix/$1
(?!...) is a negative lookahead and means not followed by. It's only a test and doesn't consume characters (this kind of elements in a pattern are also called "zero-width assertions" as a lookbehind or anchors ^ and $).
The first capture group in your pattern was useless, I removed it.

RegEx search to ignore words in a string

I have a file with thousands of entries of server names, but I want to ignore any server as shown below:
bnn6122.fdw.dee.corp;
ao.d33fegd.ao.dee.corp;
ao.d55fegd.ao.dee.corp;
qrwafgwd00846.fdw.dee.corp;
kdgf9934.wdf.dee.corp
Their values of number differ but characters stay the same.
I tried the the following code but it returns a blank list:
re.findall(r'^(?!bnn[0-9]|^ao*|^qrwafgwd[0-9]|^kdgf[0-9])\w+(.wdf.dee.corp)', f, re.M|re.I)
If I ignore the above server I should still get around 3000 servers in the list. What am I doing wrong?
You need to use a negative look-ahead anchored at the start:
^(?!(?:bnn\d+\.fdw|ao\.d\d+fegd\.ao|qrwafgwd\d+\.fdw|kdgf\d+\.wdf)\.dee\.corp)
See demo
The ^ anchor starts searching from the start of a string (use re.M if you need to search from the start of a line), then the (?!...) lookahead will make sure there are no occurrences of the substrings in the alternation group right in the beginning. The common part - dee.corp - is out of the group since it is a common ending.
Note that ao.d33fegd.ao.dee.corp and ao.d55fegd.ao.dee.corp follow the same pattern, thus it is possible to shorten the regex even more.
^(?!(?:bnn[0-9]|ao.*|qrwafgwd[0-9]|kdgf[0-9]))\w+(?:\.wdf\.dee\.corp)
Try this.See demo.Escape the ..
https://regex101.com/r/hF7zZ1/1

URL Rewrite Pattern to exclude application name from path

I'm trying to use the IIS 7 URL Rewrite feature for the first time, and I'm having trouble getting my regular expression working. It seems like it should be simple enough. All I need to do is rewrite a URL like this:
http://localhost/myApplication/MySpecialFolder
To:
http://localhost/MySpecialFolder
Is this possible? I want the regular expression to ignore everything before "myApplication" in the original URL, so that I could use "http://localhost" OR "http://mysite", etc.
Here's what I've got so far:
^myApplication/MySpecialFolder$
But using the "Test Pattern..." feature in IIS, it says my patterns don't match unless I supply "myApplication/MySpecialFolder" exactly. Does anyone know how I can update my regular expression so that everything prior to "myApplication" is ignored and the following URLs will be seen as a match?
http://localhost/myApplication/MySpecialFolder
http://mysite/myApplication/MySpecialFolder
Many thanks in advance!
SOLUTION:
I needed to change my regex to:
myApplication/MySpecialFolder
Without the ^ at the beginning and without the $ at the end.
Your regular expression is correct, the pattern will be matched against path starting after the first slash after the domain.
So only bold part will be used for matching: http://localhost/myApplication/MySpecialFolder
To limit the rewriting to specific domain you have to use Conditions section with Condition input = {HTTP_HOST}
Unless there is something radically different with regexes in IIS, you would want to take out the anchor (^) at the beginning to match.
myApplication/MySpecialFolder$
The carat ^ tells it that that is the beginning of the string and the dollar sign $ tells it to match the end. A regex like abc finds "abc" anywhere in the string, ^abc matches strings that start with "abc", abc$ matches strings that end with "abc", and ^abc$ only matches when the whole string is "abc".