in reply to Silly question about regular expressions

The regular expression

/^Dr|Lady|Lord|Miss|Mr|Mrs|Ms|Sir|$/

matches like the following separated regular expressions:

/^Dr/ or /Lady/ or ... or /Sir/ or /$/

... which, obviously is not what you want. What you want is to group all the alternative titles together and make sure that there is only the title in the string:

/^(?:Dr|Lady|Lord...|Sir|)$/

The noncapturing group (?:...) makes sure that the alternatives only extend within it, so the two anchors ^ and $ still anchor the whole string.

Replies are listed 'Best First'.
Re^2: Silly question about regular expressions
by Mariola (Initiate) on Jun 25, 2006 at 11:49 UTC
    Thanks!