This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Control characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Control characters" Watch "Control characters" New topic
Author

Control characters

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
Many thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Control characters
 
Similar Threads
Regex Doubt?
Adding binary chars to a String
textarea tag - Entering string problem
Need to tokenize a String , but i need to keep what comes between "and"
Needed help in composing an regular expression