57
votes
Accepted
Using sed to remove both an opening and closing square bracket around a string
This is easy, if you follow the manual carefully: all members inside a character class lose special meaning (with a few exceptions). And ] loses its special meaning if it is placed first in the list. ...
39
votes
Accepted
What is the difference between Nginx ~ and ~* regexes?
~: If a tilde modifier is present, this location will be interpreted as a case-sensitive regular expression match.
~*: If a tilde and asterisk modifier is used, the location block will be interpreted ...
38
votes
Accepted
Nginx wildcard/regex in location path
The = modifier in location block is an exact match, without any wildcards, prefix matching or regular expressions. That's why it doesn't work.
On your regex attempt, [a-z] matches a single character ...
20
votes
Accepted
Extracting part of the string using Ansible regex_search and save the output as a variable
You need to add a group to your regex and a second parameter that specifies which group to return:
- set_fact:
my_var: "{{ zoo_config_content.stdout | regex_search('dataDir=(.+)', '\\1') | ...
18
votes
How do I match a wildcard host in ACL lists in HAproxy?
There are cases where you need to be explicit about this, such as handling redirects for wildcard SSL with multiple levels of subdomains.
Matching end (hdr_end or -m end) or substring (hdr_sub or -m ...
18
votes
Don't need the whole line, just the match from regular expression
sed -n "s/^.*\(captureThis\).*$/\1/p"
-n don't print lines
s substitute
^.* matches anything before the captureThis
\( \) capture everything between and assign it to \1
.*$ ...
18
votes
Accepted
nginx rule - match all paths except one
I ended up using the following solution:
location ~ ^/newsletter/(.*)$ {
location ~ /newsletter/one(.*) {
// logic here
}
// logic here
}
This matches all the paths under /...
17
votes
Modify fail2ban failregex to match failed public key authentications via ssh
No regex hacking is required (at least since fail2ban 0.10.4). In /etc/fail2ban/jail.conf is the following information:
[sshd]
# To use more aggressive sshd modes set filter parameter "mode" in jail....
15
votes
Extract repository name from GitHub url in bash
Old post, but I faced the same problem recently.
The regex ^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$ works for the three types of URL.
#!/bin/bash
# url="git://github.com/some-user/...
15
votes
Accepted
Why is it unnecessary to escape "/" in Nginx regular expressions?
The reason you don't have to escape / is that / is not a delimiter.
It sounds like you are accustomed to writing regular expressions in other applications, where a delimiter is required. For instance,...
12
votes
nginx rule - match all paths except one
This works but has one flaw. It will also not match on "one" followed by any characters.
location ~ ^/newsletter/(?!one).*$ {
//configuration here
}
Although, this may be better:
location = /...
10
votes
Accepted
Redirect of all the URLs that contain 1 word in specific, but that do not contain other words
I was able to solve my problem in the following way:
location ~ /travel(.*)+/(.*)$ {
if ($uri !~ "^(.*)/(reservation|hotel|faq)(.*)"){
return 301 https://www.misite.com$request_uri;
...
10
votes
Accepted
Can LocationMatch Regex match Query String portion?
Actually it's possible since Apache 2.4 (or less) using the tag as follow :
<LocationMatch "/test/upload.js">
<If "%{QUERY_STRING} =~ /query=test/">
..
...
8
votes
Accepted
Block specific URL in HAProxy / url-encoding
As it happens, HAProxy has a converter to decode the field, making sure that your ACL will always match a given string.
url_dec
Takes an url-encoded string provided as input and returns the
...
8
votes
Nginx proxy_pass root and specific url only
location / matches any location - if you want to match only /, you should use location = /. See this document for details.
Also, your regular expression location is missing the ~ or ~* operator. But ...
8
votes
Accepted
NGINX: map and try_files not working
The try_files statement is using numeric captures from the regular expression in the location statement. The problem with numeric captures is that they are overwritten whenever a regular expression is ...
7
votes
Accepted
Using FilesMatch to target ALL files
If you really mean what you say, that is, match ALL files, that would be something like:
<FilesMatch ^>
Try, but in any case I think this looks like a very bad idea. Better to be more specific,...
7
votes
Accepted
nginx: multiple matching location blocks
There's a good article about server and location block matching here. Only one location block can match, so you're going to create a location block just for mp3 files.
location ~* \.mp3$ {
...
6
votes
Modify fail2ban failregex to match failed public key authentications via ssh
At least in openssh 7.3 the log messages also contain a port number. So I had to modify sebix's solution to the following:
^%(__prefix_line)sConnection closed by <HOST> port \d+ \[preauth\]$
6
votes
Extract repository name from GitHub url in bash
Summing up:
Get url without (optional) suffix:
url_without_suffix="${url%.*}"
Get repository name:
reponame="$(basename "${url_without_suffix}")"
Get user (host) name afterwards:
hostname="$(...
6
votes
Accepted
SELinux file path context not working with regex
Try using
HOME_DIR/\.google_authenticator -- gen_context(system_u:object_r:radiusd_google_authenticator_t,s0)
Instead. Home directories aren't necessarily in /home and this acts as a macro when you ...
6
votes
Accepted
fail2ban find matches, but does not ban
Most probably you don't have pyinotify installed on your system which cause fail2ban to fail getting log file modifications. I had the same problem and fixed it using this.
1.
Install pyinotify
yum ...
6
votes
Accepted
Ansible split string on backslash
Declare a variable with the separator to avoid the quotation&escaping alchemy. Use Single-Quoted Style; backslash '\' can be used freely. For example, the task below
- debug:
msg: "{{ ...
5
votes
Accepted
Postfix Header_Check Regex 'Does Not Match' issue
The problem is postfix use header_checks for every header line. So if there is header line like
Subject: I love you :p
and postfix will apply that regex into this line. The result is postfix will ...
5
votes
fail2ban regex not matching
Try this regex - I tested it with regexr.com and it is definitely being matched:
<HOST> - - \[\d{2}\/\w{3}\/\d{4}(:\d{2}){3} \+0000\] "POST \/xmlrpc.php HTTP\/1.0" 200
One of the issues was ...
5
votes
Accepted
apt-get install packages matching wildcards
Is it possible to use --name-only somehow with apt-get install, not apt-cache search.
By default apt-cache search searches informations from the available package names and their long description. --...
5
votes
nginx stream map with wildcard
In order to use hostnames you should specify the special hostnames parameter to map. This allows for asterisk wildcards to be interpreted in the manner you expect.
map $ssl_preread_server_name $name {
...
4
votes
nginx error log Grok pattern
I used @dr01's answer to improve the recipe for error logs in nginx 1.15 using the notice format - this answer will separate out the HTTP version and HTTP method and request.
(?<timestamp>%{...
4
votes
Modify fail2ban failregex to match failed public key authentications via ssh
On Ubuntu 18.04 with OpenSSH 7.6p1 I had to extend the "cmnfailre" rules in /etc/fail2ban/filter.d/sshd.conf by
^%(__prefix_line)sConnection closed by authenticating user <F-USER>.+</F-USER&...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
regex × 729nginx × 230
fail2ban × 71
apache-2.2 × 70
rewrite × 66
.htaccess × 57
mod-rewrite × 49
linux × 43
bash × 37
regular-expressions × 36
redirect × 35
postfix × 34
apache-2.4 × 34
grep × 28
sed × 21
logging × 18
haproxy × 18
url × 17
php × 13
web-server × 13
ansible × 13
apache2 × 13
perl × 13
powershell × 12
lighttpd × 12