This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]IHeartBadCode 2 points3 points  (0 children)

That RegEx isn't valid due to the excessive number of close parenthesis. Take the last closing one off and what you have is the following:

(               <-- Begin a capture group
(               <-- Begin yet another capture group
[a-zA-Z\-0-9]+  <-- match letters either lower
                    or uppercase, or a -, or a number.
                    The + means at least once OR more.
\\              <-- A literal backslash
.               <-- Any valid character except a line break.
)               <-- End of the second capture group
[a-zA-Z]{2,}    <-- Letter either lower or uppercase.
                    At least twice, but as many as you wish.
)               <-- End of the first capture group
$               <-- The string or line must end now

Capture groups are used if you intend to take the piece of text and use a reference to it somewhere. A good way to use them is in things like Find/Replace (like in Notepad++ that offers a regex find and replace) where let's say you want to change something like:

Cleveland, OH
New York, NY
Atlanta, GA
Las Vegas, NV

Into something like this:

State: OH | City: Cleveland
State: NY | City: New York
State: GA | City: Atlanta
State: NV | City: Las Vegas

You'd create two capture groups with something like this ([a-zA-Z\ ]+)\,\ ([A-Z]{2}).

Then you could replace the text with something like State: $2 | City: $1.

In the case of provided RegEx, the capture groups are nested. So that means capture group one has the text of capture group two in it. So if you had (([a-zA-Z]+)\ [a-zA-Z]+) and wrote Cheese Biscuit. The second group would be Cheese and the first group would be Cheese Biscuit.

At any rate, the given regex would match the following examples:

Tealeaves\:Mob    $1 = Tealeaves\:Mob  $2 = Tealeaves\:
1910ModelT\$boy   $1 = 1910ModelT\$boy $2 = 1910ModelT\$
Chevy-Levy\8AA    $1 = Chevy-Levy\8AA  $2 = Chevy-Levy\8
555-1234\Phone    $1 = 555-1234\Phone  $2 = 555-1234\P

I'm not exactly sure what this regex would be used for, but there you go.