Questions tagged [regex]

When asking regex questions, always add the tag for the specific programming language or tool (e.g., Perl, Python, or Java; vi, emacs, or ɢɴᴜ grep; etc.) you are using.

Filter by
Sorted by
Tagged with
67 votes
2 answers
106k views

Nginx location regex doesn't work with proxy pass

I'm trying to get these 2 location directives working in Nginx but I'm getting some errors back when booting Nginx. location ~ ^/smx/(test|production) { proxy_pass http://localhost:8181/...
Niels's user avatar
  • 821
43 votes
2 answers
151k views

Nginx: location regex for multiple paths

I have two locations in nginx config that work: location ^~ /media/ { proxy_pass http://backend.example.com; } location ^~ /static/ { proxy_pass http://backend.example.com; } How can I combine ...
Vlad T.'s user avatar
  • 565
40 votes
5 answers
100k views

Using sed to remove both an opening and closing square bracket around a string

I'm running this command in a bash shell on Ubuntu 12.04.1 LTS. I'm attempting to remove both the [ and ] characters in one fell swoop, i.e. without having to pipe to sed a second time. I know square ...
Xhantar's user avatar
  • 1,052
31 votes
7 answers
90k views

Don't need the whole line, just the match from regular expression

I simply need to get the match from a regular expression: $ cat myfile.txt | SOMETHING_HERE "/(\w).+/" The output has to be only what was matched, inside the parenthesis. Don't think I can use grep ...
Alex L's user avatar
  • 591
31 votes
2 answers
199k views

Nginx wildcard/regex in location path

The Nginx config I have throws 404 for .php like: ## Any other attempt to access PHP files returns a 404. location ~* ^.+\.php$ { return 404; } However I have some index.php file in subfolder ...
user avatar
21 votes
10 answers
23k views

Extract repository name from GitHub url in bash

Given ANY GitHub repository url string like: git://github.com/some-user/my-repo.git or [email protected]:some-user/my-repo.git or https://github.com/some-user/my-repo.git What is the best way in ...
Justin's user avatar
  • 5,388
20 votes
2 answers
25k views

What is the difference between Nginx ~ and ~* regexes?

What is the difference between Nginx ~ and ~* regexes? For example: if ($http_referer ~* www.foobar.net) { ... } vs if ($http_referer ~ www.foobar.net) { ... }
PartialOrder's user avatar
20 votes
2 answers
40k views

NGINX Proxy_Pass remove url substring

I have one NGINX acting as reverse proxy. I need to remove a substring string_1 from the URL, the rest of the URL is variable. Example: Origin: http://host:port/string_1/string_X/command?xxxxx ...
Pedro's user avatar
  • 677
19 votes
3 answers
66k views

How do I match a wildcard host in ACL lists in HAproxy?

I have the following lines in my haproxy.conf: acl valid_domains hdr(Host) -i mysite.com images.mysite.com docs.mysite.com admin.mysite.com redirect location http://mysite.com/invalid_domain if !...
Tom's user avatar
  • 731
19 votes
1 answer
573 views

Why bracket a single letter in a grep regex?

I've seen several instances where people are doing this: grep [f]oobar But I don't understand why that is preferable to grep foobar
hortitude's user avatar
  • 565
16 votes
6 answers
16k views

Modify fail2ban failregex to match failed public key authentications via ssh

fail2ban doesn't recognize failed public key ssh logins and I assume that this can be solved by modifying the failregex of /etc/fail2ban/filter.d/sshd.config to match the following line: <date> ...
apoc's user avatar
  • 263
16 votes
1 answer
8k views

Ack search for literal strings [closed]

I am sick of having to escape things when I want to search for part of an html tag. How can I ack search for exactly what I type without having to escape stuff? e.g. ack-grep 'console.log(foo' I ...
tester's user avatar
  • 565
15 votes
4 answers
63k views

nginx rule - match all paths except one

I am trying to match all paths that begin with /newsletter/ except one (/newsletter/one) with a regex. What I have so far: location ~ ^/newsletter/(.*)$ { // configuration here } This matches all ...
dasj19's user avatar
  • 433
14 votes
1 answer
8k views

nginx pcre_compile error when using quantifiers

I'm having an issue with aliasing. I want to append up to the first 4 digits of the file as part of the directory. (like '../123456.jpg'=>'../123/123456.jpg' and '../12.png'=>'../12/12.png') Here is ...
Echo says Reinstate Monica's user avatar
14 votes
2 answers
23k views

What are PCRE limits?

In ModSecurity there are PCRE limits exceeded errors. I know I can fix this by setting rules such as: SecPcreMatchLimit 150000 SecPcreMatchLimitRecursion 150000 But, what are these rules actually ...
user avatar
13 votes
7 answers
30k views

Renaming files in linux with a regex

I had a group of files that I'd like to consistently rename, the files are named things like "System-Log-01-01-2009-NODATA.txt" "Something-Log-01-01-2009-NODATA.txt" And I wanted them as lowercase, ...
Osama ALASSIRY's user avatar
13 votes
2 answers
13k views

Grepping for CIDR ranges

From time to time I want to grep CIDR ranges out of my Apache log files. This is easy for ranges that fall on the natural boundaries (/8, /16 and /24) but not so easy for other ranges such as /17 and ...
Ladadadada's user avatar
  • 26.6k
13 votes
3 answers
51k views

nginx rewrite append a parameter at the end of an url

I need to configure my reverse proxy so that the following parameter will be added at the end of the url: &locale=de-de This almost works: rewrite ^(.*)$ $1&locale=de-de break; However, the ...
Sascha's user avatar
  • 538
13 votes
1 answer
8k views

Why is it unnecessary to escape "/" in Nginx regular expressions?

So I've made it a thing to finally learn about regular expressions. Normally when working with regular expressions it is necessary to escape delimiters (like "/") with a "\". But when I'm using ...
Harold Fischer's user avatar
13 votes
1 answer
348 views

How do I make sec ignore a timestamp properly

I have a rule that is set up like so; In /etc/sec/rules.d I have; type=SingleWithSuppress ptype=regexp pattern=(\S+) sshd\[\d+\]: PAM \d+ more authentication failures\; logname=.* uid=.* euid=.* tty=...
Ethabelle's user avatar
  • 2,052
11 votes
2 answers
41k views

nginx simple regex location

I need to set a location param in nginx if the first 5 digits of the url are numbers. site.com/12345/ or site.com/12345 But I can't for the life of me seem to get the correct regular expression. ...
The Digital Ninja's user avatar
11 votes
1 answer
18k views

Nginx redirect all old domain subdomains to new one

I had a very long domain, so I decided to change it to a shorter and more friendly one. But since I have a lot of subdomains (in fact, I have a subdomain wildcard), I wanted to keep the subdomain ...
ranieri's user avatar
  • 233
11 votes
2 answers
20k views

How to exclude files from TAR archive using regular expressions?

I have a simple question, yet I can't find or solve the answer. I want to make a tar archive, but I want to exclude some files from it using regular expression. Example of the file to exclude is this:...
Frodik's user avatar
  • 273
10 votes
5 answers
156k views

Most simple way of extracting substring in Unix shell?

What's the most simple way to extract substring on Unix shell (with regex)? Simple means: less feature less options less study Update I realized regex itself is conflicting with simplicity, and I ...
Eonil's user avatar
  • 10.6k
10 votes
2 answers
11k views

How can I route some emails to a script in Postfix?

How do I route all emails directed to user-*@example.com (i.e. [email protected]) to a pipe command in Postfix? The idea is to create craigslist-style anonymization by assigning dynamic email ...
Nick Colgan's user avatar
10 votes
1 answer
25k views

Nginx proxy_pass root and specific url only

Working on a new version of an existing web app, I need nginx to forward root (/) and multiple specific URI to be forwarded to the v2 infrastructure (with proxy_pass) while not specified URI must be ...
cyrilv's user avatar
  • 325
9 votes
3 answers
9k views

rsync: How to exclude Dotfiles only in topmost directory?

When performing backups under Bash with rsync, I'm trying to exclude all dotfiles and hidden directories in the top directory, but not those in otherwise targeted directories. For example: /copyme.c ...
user avatar
9 votes
1 answer
16k views

Show real server name in nginx logs and params passed to fastcgi?

I have configured my nginx vhosts with a regex in server_name. nginx shows the raw regex in logs and in params passed to php-fpm (I know there's $_SERVER['HTTP_HOST'] with a real value, that's not an ...
Aurielle's user avatar
9 votes
4 answers
6k views

Searching for literal "> \" using ack-grep

I am looking for lines that literally have a greater than character (a ">") followed by a space followed by a backslash character (a "\") i.e., a line with this: > \ I thought escaping would allow ...
Stephen Gornick's user avatar
8 votes
1 answer
63k views

Extracting part of the string using Ansible regex_search and save the output as a variable

I'm having a content like below inside a file. dataDir=/var/lib/zookeeper 4lw.commands.whitelist=mntr,conf,ruok,stat syncLimit=2 I wanted to read the value for dataDir using Ansible and set it to a ...
AnujAroshA's user avatar
8 votes
4 answers
36k views

nginx rewrite base url

I would like the root url http://www.example.com to redirect to http://www.example.com/something/else This is because some weird WP plugin always sets a cookie on the base url, which doesn't let ...
ptn777's user avatar
  • 125
7 votes
2 answers
5k views

Why does this regex not work on linux?

I can't say it works on Windows but from my understanding this regex is correct and how I would write it (except maybe the ^ at the beginning) From http://www.velocityreviews.com/forums/showpost.php?...
user avatar
7 votes
1 answer
19k views

How do I format this regex so it will work in fail2ban?

I've just installed fail2ban on my CentOS server in response to an SSH brute force attempt. The default regular expressions in fail2ban's sshd.conf file do not match any entries in audit.log, which ...
chapkom's user avatar
  • 181
7 votes
6 answers
3k views

Globally Coloring the Terminal by Regex

I know many programs use color - (vim, ack, color-ls to name a few) but there are still many cases where you're stuck with black and white. There are scripts that will color output based on regular ...
Jimmy's user avatar
  • 293
7 votes
2 answers
18k views

nginx url rewriting. i just don't get it

Ok, an example url is http://www.mysite.com/?p=account&view=settings the p uri is a constant. all urls have it. the view is one of many optional uri. i've tried to understand url rewriting ...
r3wt's user avatar
  • 202
7 votes
4 answers
11k views

nginx error log Grok pattern

I am having trouble getting the following nginx error log message to parse in the grok debugger. I have a feeling there is a stupid trick that I should use but can't figure out what it may be. ...
jmreicha's user avatar
  • 790
7 votes
1 answer
193 views

Is there a tool that can top-ify an input stream?

I come across quite a few use cases where it would be very useful to take input from a (usually newline-delimited) stream and summarise it in a top-like fashion (see top, iotop, etc). A kind of on-...
tudor -Reinstate Monica-'s user avatar
6 votes
7 answers
52k views

Copy multiple folders to a single destination with robocopy

I'm looking for a solution to use robocopy to copy several folders from a directory onto a distant network share. I want to choose several folders out of a directory that contains hundreds of folders ...
SDGuero's user avatar
  • 228
6 votes
2 answers
8k views

Regex negative look-ahead is not working for mod_rewrite between different Apache versions

I have a mod_rewrite line that works under Apache 2.2, but not under Apache 1.3: RewriteRule ^(?!index.php)(?!skin/)(?!js/)(.+) /index.php?file=$1 [NC,L] If I lose the negative ...
tjbp's user avatar
  • 193
6 votes
1 answer
12k views

apt-get install packages matching wildcards

I am running Debian 8 Jessie, trying to install PHP command line intepreter from a shell script. Here is what I do: sudo apt-get install php-cli it tells me there is no php-cli. I do find there is ...
xrfang's user avatar
  • 195
6 votes
5 answers
13k views

apache locationmatch negative regex

I'm trying to expose a location through Apache. Normally, I have this block in my vhost that says <Location /> AuthType Basic AuthUserFile /web/.htpasswd AuthName "Test Site" ...
Jeff's user avatar
  • 61
6 votes
1 answer
657 views

I can not seem to get a Regex for to Fail2Ban to work with Opensmtpd? Can I get a sanity check?

I have openSMPTD running a mail server. It seems I am getting brute forced attacked currently, although I doubt they will get in. That being said, I would like to alleviate the burden on my server ...
David Frick's user avatar
6 votes
1 answer
4k views

nginx regex characters that require quoting?

So I was configuring nginx today and I hit a weird problem. I was trying to match a location like this: location ~ ^/([0-9]+)/(.*) { # do proxy redirects } ...for URLs like "http://my.domain.com/...
Michael Louis Thaler's user avatar
5 votes
3 answers
2k views

Bash command for regular expression substitution

What bash command can be used for regex substitution on a pipe? cat foo.txt | someprogram
Alex's user avatar
  • 2,387
5 votes
1 answer
7k views

Regex for multiple sshd Received disconnect from ... [preauth]

What fail2ban regex would catch logs like these? Apr 9 08:48:28 server sshd[1856]: Received disconnect from 43.255.190.117: 11: [preauth] Apr 9 09:06:05 server sshd[1936]: Received disconnect ...
JB0x2D1's user avatar
  • 161
5 votes
1 answer
17k views

Regular expressions in server_name with Nginx vhost

I have something I'm trying to do with Nginx. Say I have a few users and a subdomain, and each user has a directory at /home/dev/sites/USER_ID. I would like for USER_ID.dev.example.com to point to ...
SharkofMirkwood's user avatar
5 votes
3 answers
22k views

Regex for sed to grab multiple lines or a better way?

I'm creating a script that connects to a server and dumps the output to a tempfile. I want to use sed in the script to grab specific information from the temp file. The output would always have the 80 ...
krizzo's user avatar
  • 387
5 votes
2 answers
4k views

fail2ban find matches, but does not ban

I am using fail2ban 0.9.7 on CentOS 7 along with an Apache reverse proxy, trying to ban bots trying to access my server as an open proxy, such as : 221.8.179.164 - - [10/Jun/2019:22:04:19 +0200] "...
ojathelonius's user avatar
5 votes
2 answers
5k views

Is it possible to get the matched regex from within the apache LocationMatch directive?

I'm wondering if the following code should be working: <LocationMatch "/(.*)([/])?(.*)"> Order allow,deny Allow from all AuthType Basic AuthName "Git" AuthUserFile /git/....
tftd's user avatar
  • 1,508
5 votes
1 answer
2k views

NGINX: map and try_files not working

i am using nginx to detect webp support in browser: # Check if client is capable of handling webp map $http_accept $webp_suffix { default ""; "~*webp" ".webp&...
forke's user avatar
  • 143

1
2 3 4 5
15