Questions tagged [regular-expression]

Regular expressions are a means of matching a pattern of characters within a string.

Regular expressions (regex or regexp for short) are patterns that describe a set of strings based on simple syntactic criteria.

Tools such as , and involve regular expression matching as a key feature. Most programming languages include a regular expression library, and most text editors provide regular expression search and replacement.

Shell for file names (also called glob patterns) are a different, less powerful syntax (though ksh, bash and zsh have more powerful extensions). Bash, ksh93 and zsh's =~ operator offer regex matching.

External reference

Further reading

2769 questions
10
votes
2 answers

Matching special characters with Regular Expression

Say it's very easy if I want to find something containing lower-case letters and numbers with produce_text | grep -E '[0-9a-z]' Brackets are useful to match a set of characters, but what about those that are somewhat special? If I want to, using…
iBug
  • 3,508
9
votes
2 answers

What is the definition of a regular expression?

I recently got into a friendly argument with Ghoti about what constitutes a regular expression in the comments to my answer to this question. I claimed that the following is a regular expression: `[Rr]eading[Tt]est[Dd]ata` Ghoti disagreed, claiming…
terdon
  • 242,166
5
votes
4 answers

Select everything between two timestamps in a log file

awk '/10:..:/, /13:..:/' server.log > /tmp/awktmp I tried this command as someone gave me. It worked for me to find logs between 10:00 AM to 13:00 PM but I don't understand it fully. Please tell me an elegant solution if you've one. Remember the…
5
votes
1 answer

What are the differences between the regular expression engines such as emacs and posix-egrep?

The GNU implementation of the find command uses "Emacs Regular Expressions" by default for its -regex predicate. This can be changed to options such as posix-egrep. What are the differences in each engine in addition to syntaxes? For example, do…
Ryan
  • 177
5
votes
4 answers

Are Blanks, Spaces, and tabs part of a string?

Is there a difference between a space and a blank? Are tabs, spaces, blanks considered characters?
5
votes
0 answers

Renaming files with vidir (moreutils)

I'm trying to use vidir from the moreutils package and I have the following issue: When I load the files I want to rename with vidir like this: $ vidir or like this: $ find -type f | vidir --verbose - vidir displays a number, a tab, and the full…
5
votes
2 answers

regex wanted to exclude a string

I want to have an expression that targets all lines that begin with http, end with icon.ico and do not contain config.privoxy.org. In the sample list below, I would like to catch all except the third and fourth entries (from the…
user15760
5
votes
4 answers

Delete folders whose name has 4 digit numbers two times

I want to delete folders using regexp in a Mac terminal. 0129_0140 (no delete) 0140_0140 (delete) 0150_0160 (no delete) 0170_0170 (delete) I just want to delete folders such as 0140_0140 , 0170_0170 . (Added) I want to delete the nonempty folders,…
jakeoung
  • 161
4
votes
4 answers

Bash regex whitespace before match

I'm trying to match a line in a textfile with if [[ ${regel} =~ ([\s][CN][G]{2}[A]{2}[T]) ]]; I also tried instead of /s to use /A and /b couple examples of things I tried: if [[ ${regel} =~ (\A[CN][G]{2}[A]{2}[T]) ]]; if [[ ${regel} =~…
4
votes
2 answers

Why is this number matched with this regex?

The regex is -?([0-9]|([1-9][0-9])). The number is -2231 and it's being matched. From my understanding, it should be a single digit or double digits.  Why is this number matched with this regex?
Rashad
  • 141
4
votes
3 answers

Using regex to get mouse MAC address

What would be the most handy way to grab the mouse's mac address from the following output: ~ ➜ bt-device --list Added devices: Logitech K811 (00:1F:20:EB:06:E0) Plattan ADV Wireless (5C:EB:68:1F:D1:62) Bluetooth Mouse M336/M337/M535…
4
votes
1 answer

Regex : how to verify that there are 13 greek letters with an odd number of consonants

Another regex that I can't seem to crack :( I tried with egrep '([qwrtzpsdfghjklxcvbnmy]{1})|([qwrtzpsdfghjklxcvbnmy]{3})|([qwrtzpsdfghjklxcvbnmy]{5})|([qwrtzpsdfghjklxcvbnmy]{7})' greek.txt However this also returns words with 4 consonants, I do…
3
votes
1 answer

Regex search list of emails

So I have a long list of email and I want to search these based on just a few sections of the email string. I want to try and search these emails more efficently so I want to match the start of the email. Is there a simple way to match the first…
Callum
  • 133
3
votes
1 answer

Why is regular expression matching so slow

I learned from this article about ripgrep that regular expression engines that implement backtracking can be very slow in some cases, but I don't really understand why. Could someone explain in simple terms why the following python snippet, given as…
3
votes
3 answers

Regular expression that ignores certain characters

I need to find a regular expression, that ignores certain characters for usage in the bib2bib tool. For example: I need to find any occurence of the word "muller". But also a string like ''Hello, my name is Michael M\"uller, how are you?'' or …
1
2 3
8 9