Reputation: 4607
I'm making an application that asks the user to enter a postcode and outputs the postcode if it is valid.
I found the following pattern, which works correctly:
String pattern = "^([A-PR-UWYZ](([0-9](([0-9]|[A-HJKSTUW])?)?)|([A-HK-Y][0-9]([0-9]|[ABEHMNPRVWXY])?)) [0-9][ABD-HJLNP-UW-Z]{2})";
I don't know much about regex and it would be great if someone could talk me through this statement. I mainly don't understand the ?
and use of ()
.
Upvotes: 2
Views: 3603
Reputation: 5236
Your regex has the following:
^
and $
- anchors for indicating start and end of matching input.[A-PR-UWYZ]
- Any character among A to P or R to U or W,Y,Z. Characters enclosed in square brackets form a character class, which allows any of the enclosed characters and -
is for indicating a sequence of characters like [A-D] allowing A,B,C or D.([0-9]|[A-HJKSTUW])?
- An optional character any of 0-9 or characters indicated by [A-HJKSTUW]. ?
makes the preceding part optional. |
is for an OR
. The ()
combines the two parts to be OR
ed. Here you may use [0-9A-HJKSTUW]
instead of this.[ABD-HJLNP-UW-Z]{2}
- Sequence of length 2 formed by characters allowed by the character class. {2}
indicates the length 2. So [ABD-HJLNP-UW-Z]{2}
is equivalent to [ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]
Upvotes: 2
Reputation: 473
the ? means occurs 0 or 1 times and the brackets do grouping as you might expect, modifiers will work on groups. A regex tutorial is probably the best thing here
http://www.vogella.com/articles/JavaRegularExpressions/article.html
i had a brief look and it seems reasonable also for practice/play see this applet
http://www.cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html
simple example (ab)?
means 'ab' once or not at all
Upvotes: 2