Questions tagged [regex]

Regex (or regexp) is known as regular expression matching of patterns, strings or characters in, for example, a large text file. Questions should be tagged as such whatever the programming language involved and the tag can also apply to command-line or graphical programs that have regex plugins or some regex capability.

The syntax of regular expressions varies between programming languages, and the formulations developed can vary from the simple to the ultra-complex. Probably the simplest form of regex is what takes place in the shell when a search is performed, for example for files matching *.jpg, and all jpgs within the current working directory are found.

At this site the different types of regex are introduced; Basic Regular Expressions (BRE), Extended Regular Expressions (ERE) and Perl-compatible Regular Expressions (PCRE). However, different programs will have different regular expression capabilities. Shell programs such as grep and sed have special switches that enable them to use more advanced regular expressions; grep, for example, can use ERE with the -E switch and PCRE with the -P switch.

A very useful general introduction to regex and its use in the shell is in chapter 20 of Linux Command and Perl regular expressions are documented in great detail in the official documentation.

313 questions
191
votes
4 answers

How to grep for tabs without using literal tabs and why does \t not work?

When I search for tabs in a file with (e)grep I use the litteral tab (^v + ). I can not utilize \t as a replacement for tabs in regular expressions. With e.g. sed this expression works very well. So is there any possibility to use a…
Lasall
  • 3,733
124
votes
12 answers

How to search for all the files starting with the name "ABC" in a directory?

I need to search for files starting with some particular name. There can be multiple files starting with a particular pattern and I want to list all such files present in the directory.
R11G
  • 1,573
66
votes
3 answers

Pattern based, batch file rename in terminal

I need to rename the following: file_001_loremipsum.png file_002_dolor.png file_003_sit.png file_004_amet.png file_105_randomness.png into upl_loremipsum.png upl_dolor.png upl_sit.png upl_amet.png upl_randomness.png How do I make it happen with…
57
votes
6 answers

apt-get remove with wildcard removed way more than expected. why?

Last night I was trying to burn CDs. Being annoyed with k3b and choosing to use brasero instead, I went to remove k3b. I typed in: sudo apt-get remove k3b I hit tab twice and saw that I had both k3b and k3b-data on my system. Assuming that I…
52
votes
4 answers

How to specify a filename while extracting audio using youtube-dl?

I can create an mp3 of a YouTube video with the following command: youtube-dl --extract-audio --audio-format mp3 http://www.youtube.com/watch?v=rtOvBOTyX00 It creates an mp3 with the following filename: Christina Perri - A Thousand Years [Official…
38
votes
4 answers

sed with PCRE (like grep -P)

I am happy that grep does support Perl Compatible Regular Expressions with the -P option. Is there a reason why the tool sed does not have this feature?
guettli
  • 1,765
33
votes
1 answer

How to include a space character with grep?

I have a file named example $ cat example kali.pdf linux.pdf ubuntu.pdf example.pdf. this.pdf grep .pdf and when I use grep to get the line that has a space before .pdf, I can't seem to get it. grep *.pdf example returns nothing, (I want to say,…
solfish
  • 1,591
31
votes
3 answers

Difference between '.' , '?' and '*' in regular expressions?

Could I get an example as to how these three elements (are these called metacharacters?) differ? I know that * means all or nothing, but I am not sure if it's the right way to think about it. On the other hand . and ? seem the same. They match one…
posixKing
  • 1,143
29
votes
1 answer

Using Modifiers of Perl-compatible Regex (PCRE) in grep

According to grep --help and man grep, we can use the -P option in order to interpret the pattern as a Perl regular expression (PCRE, to be precise), instead of the default POSIX basic regular expressions (BRE). In Perl language, various Modifiers…
24
votes
2 answers

Regular expressions VS Filename globbing

I know that Regular expressions are to be used only with characters and strings, but sometimes I find them in the names of files. My question is: are regexes only used with characters or also with filenames?
Hamza
  • 387
22
votes
6 answers

Grep beginning of line

I have a file with the following contents: (((jfojfojeojfow // hellow_rld (((jfojfojeojfow // hellow_rld How can I extract every line that starts with a parenthesis?
19
votes
4 answers

Grep: The asterisk (*) doesn't always work

If I grep a document that contains the following: ThisExampleString ...for the expression This*String or *String, nothing is returned. However, This* returns the above line as expected. Whether the expression is enclosed in quotes makes no…
Trae
  • 811
17
votes
2 answers

What do ^$ and ^# mean?

I don't understand BADIPS=$(egrep -v "^#|^$" $tDB). Can you explain it? full code: #!/bin/bash # Purpose: Block all traffic from AFGHANISTAN (af) and CHINA (CN). Use ISO code. # # See url for more info - http://www.cyberciti.biz/faq/?p=3402 #…
17
votes
1 answer

Capture groups are ignored when renaming files

I have a number of files in this format: ##.## - File name.mp4 I want to rename them to: s##e##.mp4 This is what I came up with: rename -n "s/^(\d{2})\.(\d{2}).*/s$1e$2.mp4/" It works when I tested it on http://regexr.com/ but when I run the command…
0x0049
  • 273
17
votes
8 answers

Recommendation for Regex editor?

I asked for recommendations for Regex editors on stackoverflow a while ago. Following is one of the replies: What is "good" depends on what is most useful to you. For me, though, these are the key features for a good regex editor (besides…
Tim
  • 26,107
1
2 3
20 21