regix match words starting with dot but exclude some - regex

I need to build regex to match all words starting with . but also white flag words like .well-known .. or some similar..
For now I have build the one that's complete opposite,, this one captures ONLY this one. I tried to find some regex symbol for invert but that doesn't exist I think..
location ~ /^(\.well-known) {
deny all;
}
thx

Here's an expression that works:
^(\.(?!well-known|other-forbidden-words|another-forbidden-word).+)$
Simply change the other words to white-listed words you want, and add more if need be.

hm,,
I guess that I found my answer thanks to:
Invert match with regexp
this is the test that works now:
https://regex101.com/r/mS2wC6/2
and solution is this:
^(\.(?!well-known))
so ok, I think I got this grouping thing now..
hth, k

well you don't have to add words in OR "|" condition
you can simply do
$pattern = "/\.+(?:[a-zA-Z]|-)/";
which accepts anything that starts with . and contains - or alphabets.

Related

RegEx substract text from inside

I have an example string:
*DataFromAdHoc(cbgv)
I would like to extract by RegEx:
DataFromAdHoc
So far I have figured something like that:
^[^#][^\(]+
But Unfortunately without positive result. Do you have maybe any idea why it's not working?
The regex you tried ^[^#][^\(]+ would match:
From the beginning of the string, it should not be a # ^[^#]
Then match until you encounter a parenthesis (I think you don't have to escape the parenthesis in a character class) [^\(]+
So this would match *DataFromAdHoc, including the *, because it is not a #.
What you could do, it capture this part [^\(]+ in a group like ([^(]+)
Then your regex would look like:
^[^#]([^(]+)
And the DataFromAdHoc would be in group 1.
Use ^\*(\w+)\(\w+\)$
It just gets everything between the * and the stuff in brackets.
Your answer may depend on which language you're running your regex in, please include that in your question.

How to use RegEx to add "_" between two words with notepad++

I want to use Notepadd++ replace option with regular expression to accomplish this:
From: IntegrationName
To: Integration_Name
How can do this ?
My RegEx to search is: .[A-Z]
this finds: "oN"
But I don't know what to put in the replace box so it will only add "_" between the "o" and the "N"...
Another solution using lookaround assertions would be:
(?<=[a-z])(?=[A-Z])
and replace with
_
Note: The "Match case" option needs to be active, otherwise Notepad++ will find a match between every two letters.
This regex will find every position where a lowercase is on the left and an uppercase is on the right.
You can make use of capture groups. If I have to take your current attempt and edit it as little as possible, you would get:
(.)([A-Z])
This will store the match of . into $1 and the uppercase letter in $2, thus, you can use the following in the replace entry box:
$1_$2
I know you've accepted an answer, but when I ran it, I got From: _Integration_Name
Here's my idea;
(:\s)(.{1})([a-z]*)([A-Z]{1})
And use the following replace
$1$2$3_$4
I finaly did it like this:
Find: ([a-z])([A-Z])
Replace with: $1_$2

Replace multiple sentences between 2 expressions in multiple files Notepad ++

I have 58K files where I need to find this expression
()">A Random sentence.</A></P>
and i need to replace A Random Sentence by nothing.
I was trying on Notepad++ something like
Find What: ()">[[:alnum:][:punct:][:space:]]</A></P>
Replace: <empty>
Not even gettng results from the search...
Waiting for some feedback.
Try to find
(\(\)">).*(<\/A><\/P>)
and replace it with
$1\<empty\>$2
The idea is to save left part and right part, placing essential parts in brackets ().
The ".*" means every character in between.
In replace statement we call $1 and $2 to access saved parts.
You also can try :
(?<=\(\)">)[a-z \.-]+(?=</A></P>)
here [a-z \.-] you put everything what you want to search
Also parenthesis in Notepad++ should be mark with \
This should work for you:
Find: (?<=\(\)">)A Random sentence.(?=<\/A><\/P>)
Replace: <empty>
If A Random sentence. is not the actual sentence you can replace the find with:
(?<=\(\)">).*?(?=<\/A><\/P>)

Pattern matching in Perl

I am doing pattern match for some names below:
ABCD123_HH1
ABCD123_HH1_K
Now, my code to grep above names is below:
($name, $kind) = $dirname =~ /ABCD(\d+)\w*_([\w\d]+)/;
Now, problem I am facing is that I get both the patterns that is ABCD123_HH1, ABCD123_HH1_K in $dirname. However, my variable $kind doesn't take this ABCD123_HH1_K. It does take ABCD123_HH1 pattern.
Appreciate your time. Could you please tell me what can be done to get pattern with _k.
You need to add the _K part to the end of your regex and make it optional with ?:
/ABCD(\d+)_([\w\d]+(_K)?)/
I also erased the \w*, which is useless and keeps you from correctly getting the HH1_K.
You should check for zero or more occurrences of _K.
* in Perl's regexp means zero or more times
+ means atleast one or more times.
Hence in your regexp, append (_K)*.
Finally, your regexp should be this:
/ABCD(\d+)\w*_([\w\d]+(_K)*)/
\w includes letters, numbers as well as underscores.
So you can use something as simple as this:
/ABCD\w+/

Quick Regex question

Can anybody guide me in the right direction...
I have some folder strucktures that I want to check for a trailing / slash, some of the folders don't have a trailing / and if it does not have a trailing
/ I need to add one
If I use this regex it works but it replace the last character of the folder name
Folder/name/test
folder/name/test1/
folder/name/test2/
replace(/.$/ig,"/");
This regex replaces Folder/name/tes[t]/ but will take out the t and replace it with /
Hope this makes sense...
Try something like this:
replace(/[^/]$/ig, "$0/")
replace(/(.)$/ig,"\1/");
or better
replace(/([^\\])$/ig,"\1/");
if \1 isn't a backreference in your language, then you'll have to figure that out, or tell us teh language.
The regex you made basically means this: take any character that is the last one in the string and replace it with /. What you need to do is to group the last character and then insert it again in the replacement, like this:
replace(/([^\/])$/ig,"$1/");
For more information see
http://www.regular-expressions.info/brackets.html
Without knowing the language it's difficult to post a correct answer and you can't use the code provided in a cut-and-paste fashion. Anyway I might go for this regex:
replace(/(.)\/*$/,"\1/");
This will append the trailing / only if it's not there yet.
I'm not sure which language this is for but this is how you would do it in Perl:
#! /local/bin/perl
my #data = <data>;
while (<DATA>)
{
s#[^/]\n#/\n#m;
print;
}
__DATA__
/foo/bar/
/baz/jazz
/baz/jazz
This then prints out the following:
/foo/bar/
/baz/jaz/
/baz/jazz/
The key to the regex is the "[^/]\n" This basically matches anything at the end next to to the newline. With your nomenclature, I would assume the syntax would be the following:
replace(/[^\/]\n/ig,"/");
Or if there is no newline use this:
replace(/[^\/]$/ig,"/");
I hope that helps.
I would avoid a regular expressions in this case and do something easier like:
$path = rtrim($path, '/').'/';
EDIT:
Woops, assumed it was php...