Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between () and [] in regular expression patterns?

Tags:

regex

What is the difference between encasing part of a regular expression in () (parentheses) and doing it in [] (square brackets)?

How does this:

[a-z0-9] 

differ from this:

(a-z0-9) 

?

like image 332
KatieK Avatar asked Sep 24 '10 17:09

KatieK


People also ask

What is [] in regular expression?

The [] construct in a regex is essentially shorthand for an | on all of the contents. For example [abc] matches a, b or c. Additionally the - character has special meaning inside of a [] . It provides a range construct. The regex [a-z] will match any letter a through z.

What is the purpose of the curly brackets {} in regular expression?

The curly brackets are used to match exactly n instances of the proceeding character or pattern. For example, "/x{2}/" matches "xx".

What is the difference between regex and pattern?

Pattern matching is used by the shell commands such as the ls command, whereas regular expressions are used to search for strings of text in a file by using commands, such as the grep command.

What is the difference between .*? And * regular expressions?

represents any single character (usually excluding the newline character), while * is a quantifier meaning zero or more of the preceding regex atom (character or group). ? is a quantifier meaning zero or one instances of the preceding atom, or (in regex variants that support it) a modifier that sets the quantifier ...


2 Answers

[] denotes a character class. () denotes a capturing group.

[a-z0-9] -- One character that is in the range of a-z OR 0-9

(a-z0-9) -- Explicit capture of a-z0-9. No ranges.

a -- Can be captured by [a-z0-9].

a-z0-9 -- Can be captured by (a-z0-9) and then can be referenced in a replacement and/or later in the expression.

like image 100
Jeff Rupert Avatar answered Oct 04 '22 13:10

Jeff Rupert


(…) is a group that groups the contents like in math; (a-z0-9) is the grouped sequence of a-z0-9. Groups are particularly used with quantifiers that allow the preceding expression to be repeated as a whole: a*b* matches any number of a’s followed by any number of b’s, e.g. a, aaab, bbbbb, etc.; in contrast to that, (ab)* matches any number of ab’s, e.g. ab, abababab, etc.

[…] is a character class that describes the options for one single character; [a-z0-9] describes one single character that can be of the range az or 09.

like image 32
Gumbo Avatar answered Oct 04 '22 14:10

Gumbo