Jump to content

Regex matching

- - - - -

  • Please log in to reply
2 replies to this topic

#1
ch3etah

ch3etah

    Newbie

  • Members
  • Pip
  • 8 posts
I'm trying to match a string using regex. The format is like this:

[AN][AN][N][N][N][AN][AN] where AN is alphanumeric upper case character and N is a numeric character
What I'm doing is to save the string into a variable and then check it against a define pattern:

myPattern.matches("([A-Z0-9]{2})([0-9]{3})([A-Z0-9]{2})");

Apparently this is not the way to do it as it matches strings like "AZS..34"

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Edit: (Deleted my previous post)

I'm not certain, but I would try removing the parentheses, since those are capturing groups and are not needed because you aren't using back references.

Never done Regular Expressions in Java, though, so unsure. Usually use Perl or C for this.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
ch3etah

ch3etah

    Newbie

  • Members
  • Pip
  • 8 posts
Many thanks Greg. I should learn to read the API :D
To solve the problem:

Pattern.matches("([A-Z0-9]{5}+[0-9]{6})", "myPattern")

it works smoothly like this.

Thanks again




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users