문제

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.

도움이 되었습니까?

해결책

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"

다른 팁

Use the following character classes:

[1-9TJQK][SDCH]

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top