tags:

views:

324

answers:

4

I have this problem:

The text "ABCD\r\nEFGHJ" loaded from a file is matched with java regex "EFGH". Matcher object of course says start of the matched string is in position 6. The matcher counts \r \n as two positions.

I put the original text in a AWT TextArea Component and then call select(6,10) to highlight the area which was matched. Guess what... it starts highlighting from 'F' letter. One position forward than it should...

If more than 1 pair of crlf precedes matched area then highlighting moves even more forward than it should.

Anyone has any simple solution?

+1  A: 

Simple solution: remove all \r from the text... :-P

Not as stupid as it sounds, unless you have inconsistent end of lines (it can happen) and want to keep them unchanged... And that's probably what the component does anyway.

PhiLho
ColdFusion event has a builtin function for that. Safe bet.
Tomalak
A: 

I cant mess with the text because it is protocol data and \r and \n characters have semantics that dont have to do with display or line separation. I just want a component that will treat each one input character separately and treat it as one displayed and counted, no matter how it is displayed.

Paralife
A: 

If the \r\n are consistent, you can remove the \r's before running the regex, then replace them before handing off to whatever is next. Or change a copy, if that works better. This way, your regex finds the position in a way consistent with what AWT is expecting.

Ben Doom
A: 

how can make stack operation using the applet java can any one sent to me the information please i need help