Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I have some questions about java.util.regex

 
chrislxd roger
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to find Hexadecimal number from a String.So I write this code



But Print is "MATCH:",I don't know where trouble is?Can somebody helps me,please?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not how the \xhh feature works. It doesn't match the hex notation like "0x56", it matches the actual hex character. And the character to match would be specified using the two "hh" digits. So if you want to match the character "A" like this, you use "\x41".

Matching the notation can be done with something like "0x[\da-fA-F]{2}".
 
chrislxd roger
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!!!It works.But I really want to know where to use "\xhh".May be an example.Thank you again!
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this:

Note that hex 41 = decimal 65 = ASCII 'A'.

Of course, for 'A' this is not very useful, because A matches itself if you include it directly in a regexp. But for non-printable control characters that do not have a special regexp pattern (like vertical tab hex 0B) this is useful.
 
chrislxd roger
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh~I see.I appreciate your detailed explanation. ^_^
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic