Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Regualr Expression Puzzle (http://www.velocityreviews.com/forums/t391014-regualr-expression-puzzle.html)

 Ken 02-21-2007 04:18 PM

Regualr Expression Puzzle

Here's my pattern: "\\b[A-Z]([A-Z0-9]|([+-_/&](?=[A-Z0-9])))+\\b"

This pattern finds the string F1.3 . For the life of me I can't figure
out how the dot gets sucked in. Seems like it should find F1 . Can
someone explain?

Ken

 Ben 02-21-2007 04:47 PM

Re: Regualr Expression Puzzle

Ken wrote:
> Here's my pattern: "\\b[A-Z]([A-Z0-9]|([+-_/&](?=[A-Z0-9])))+\\b"
>
> This pattern finds the string F1.3 . For the life of me I can't figure
> out how the dot gets sucked in. Seems like it should find F1 . Can
> someone explain?
>
> Ken
>

"[+-_/&]" is the set '+' to '_' plus '/' plus '&'. I'm not sure what you
are doing but you probably mean "[-+_/&]" .

 Oliver Wong 02-21-2007 04:49 PM

Re: Regualr Expression Puzzle

"Ken" <spam@kenkast.com> wrote in message
> Here's my pattern: "\\b[A-Z]([A-Z0-9]|([+-_/&](?=[A-Z0-9])))+\\b"
>
> This pattern finds the string F1.3 . For the life of me I can't figure
> out how the dot gets sucked in. Seems like it should find F1 . Can
> someone explain?

Did you mean:

"\\b[A-Z]([A-Z0-9]|([+\\-_/&](?=[A-Z0-9])))+\\b"

?

- Oliver

Re: Regualr Expression Puzzle

On Feb 21, 11:18 am, "Ken" <s...@kenkast.com> wrote:
> Here's my pattern: "\\b[A-Z]([A-Z0-9]|([+-_/&](?=[A-Z0-9])))+\\b"
>
> This pattern finds the string F1.3 . For the life of me I can't figure
> out how the dot gets sucked in. Seems like it should find F1 . Can
> someone explain?
>
> Ken

Hey Ken,
The period character is 0x46. In your expression, you stated '[+-_'
which means "match any character between '+' (0x43) and '_' (0x95)".
Since '.' (0x46) is within this range, it gets matched. '-' and '+'
should be escaped, though switching the order like Oliver did
obviously works as well.

Here's a great resource for understanding how the regex engines work:
http://www.regular-expressions.info