AWT TextArea 似乎将 cr-lf 序列计为一个字符
我有这个问题:
从文件加载的文本“ABCD\r\nEFGHJ”与java正则表达式“EFGH”匹配。 当然,匹配器对象表示匹配字符串的开头位于位置 6。匹配器将 \r \n 计为两个位置。
我将原始文本放入 AWT TextArea 组件中,然后调用 select(6,10) 以突出显示匹配的区域。 你猜怎么着......它从“F”字母开始突出显示。 比应有的位置向前移动一个位置...
如果超过 1 对 crlf 在匹配区域之前,则突出显示会比应有的位置向前移动更多。
有人有任何简单的解决方案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单的解决方案:从文本中删除所有 \r... :-P
并不像听起来那么愚蠢,除非您的行尾不一致(可能会发生)并且希望保持它们不变...这可能就是组件的内容无论如何。
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.
我不能弄乱文本,因为它是协议数据,而 \r 和 \n 字符具有与显示或行分隔无关的语义。 我只想要一个能够单独处理每个输入字符并将其视为一个显示和计数的组件,无论它如何显示。
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.
如果 \r\n 一致,您可以在运行正则表达式之前删除 \r,然后在切换到下一步之前替换它们。 或者更改副本,如果效果更好的话。 这样,您的正则表达式就会以与 AWT 期望一致的方式找到位置。
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.