Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Control characters

 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I want to include ASCII control characters in my string.
Control characters such as STX = 0x02, ETX = 0x03, etc.
I have tried:



and then I want to match them against a matcher:



But it doesn't work. Can someone help? Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your pattern is incorrect. It looks for 0x02 followed by a ? followed by 0x1f followed by a ? followed by 0x03. After all, "\\?" is the Java String representation of \? which is the regex for a single ?.

Wait, not even that. Your 0x02, 0x1f en 0x03 are all ints. That means that your regex will be "2\\?31\\?3". You'll need to use the Unicode values for this: "\u0002\\?\u001f\\?\u0003". But then of course with the \\? parts replaced by the proper sequences. You probably want .*? - any number of characters in a non-greedy fashion. You'll also want to use those Unicode parts in your input line, because that too uses simple int concatenation: 2192.168.1.131BBC3. And you want to declare them as char instead of int, or they will be turned back into 2, 31 and 3 again.

Also note that matches uses exact String lookup. It will return true only if the entire input String matches the regex. So if the regex is ".", then matches only returns true for single character Strings.
 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic