Question

Quick question.

What's the code for Java regex that matches a string that has {1,2,3,4,5,6,7,8,9,T,J,Q,K} on the 1st char and {S,D,C,H} on the second. How can I do that?

Thanks.

Was it helpful?

Solution

Very simple:

'^[1-9TJQK][SDCH]$'

Note that using ^ and $ means that the string contains only two chars and respect the rules you need.

As Jonathon Reinhart points out, maybe my one-line explanation is not enough. Obviously you should study in deep what that sign means. Anyway a good start point is his description:

  • ^ means "at start of string"
  • [1-9TJQK] means "any one character between 1 and 9 (inclusive) or T or J or Q or K
  • [SDCH] Same logic as before
  • $ means "at end of string"

OTHER TIPS

Use the following character classes:

[1-9TJQK][SDCH]

To learn more, please visit http://www.regular-expressions.info/quickstart.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top