使用 JTextField 从用户获取正则表达式。如何让它将 \t 视为制表符而不是 \ 后跟 at
JTextField reSource; //contains the regex expression the user wants to search for
String re=reSource.getText();
Pattern p=Pattern.compile(re,myflags); //myflags defined elsewhere in code
Matcher m=p.matcher(src); //src is the text to search and comes from a JTextArea
while (m.find()==true) {
- 如果用户输入
\t
,它会找到\t
而不是 Tab。 - 如果用户输入
\\\t
,它会找到\\\t
不是制表符。 - 如果用户输入
[\t]
或[\\\t]
,则会找到t< /code> 不是制表符。
我希望它能够在用户输入 \t
时找到选项卡。当然,它还需要与 \n
、\r
等一起使用...
如果使用 re="\t";
而不是re=reSource.getText();
在 JTextField
中使用 \t
然后它会找到选项卡。如何让它与 JTextField
的内容一起工作?
JTextField reSource; //contains the regex expression the user wants to search for
String re=reSource.getText();
Pattern p=Pattern.compile(re,myflags); //myflags defined elsewhere in code
Matcher m=p.matcher(src); //src is the text to search and comes from a JTextArea
while (m.find()==true) {
- If the user enters
\t
it finds\t
not tab. - If the user enters
\\\t
it finds\\\t
not tab. - If the user enters
[\t]
or[\\\t]
it findst
not tab.
I want it such that if the user enters \t
it finds tab. Of course it also needs to work with \n
, \r
etc...
If re="\t";
is used instead of re=reSource.getText();
with \t
in the JTextField
then it finds tabs. How do I get it to work with the contents of the JTextField
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
示例:
输出:
试试这个:
我认为问题在于理解当您输入:
那么它实际上与:
但如果您输入:
那么 System.out.print(str) 将是“\t”。
Example:
Output:
Try this:
I think the problem is in understanding that when you type:
Then it is actualy same as:
But if you type:
Then the System.out.print(str) will be "\t".
匹配
\t
应该可以工作,但是,您的标志可能有问题。这对我有用:
输出是:
Matching
\t
should work, however, your flags might have a problem.Here's what works for me:
Output is:
我的经验是,Java Swing JTextField 和 JTable GUI 控件通过添加反斜杠前缀来转义用户输入的反斜杠。
用户输入两个字符序列“反斜杠t”,控件的getText()方法返回一个包含三个字符序列“反斜杠反斜杠t”的字符串。 SO 格式化程序对文本中的反斜杠执行自己的操作,因此这里是代码:
对于三字符输入序列“backsl backsl t”,getText() 返回五字符序列“backsl backsl backsl backsl t”。作为代码:
这基本上可以防止反斜杠修改 t 以产生在由 System.out.println 等解释时变成制表符的字符序列。
方便且令我惊讶的是,正则表达式处理器无论如何都接受它。两个字符序列“\t”与制表符匹配,三个字符序列“\\t”也是如此。请参阅下面的演示代码。 system.out 调用演示了哪些序列和模式具有制表符,并且在 JDK 1.7 中,两者匹配都会产生 true。
我的 JDK1.7 系统上的输出如下,输出中的选项卡可能无法在 SO 格式化程序中保存:)
HTH
My experience is that Java Swing JTextField and JTable GUI controls escape user-entered backslashes by prefixing a backslash.
User types two-character sequence "backslash t", control's getText() method returns a String containing the three-character sequence "backslash backslash t". The SO formatter does its own thing with backslashes in text so here it is as code:
For three-character input sequence "backsl backsl t", getText() returns the five-character sequence "backsl backsl backsl backsl t". As code:
This basically prevents the backslash from modifying the t to yield a character sequence that becomes a tab when interpreted by something like System.out.println.
Conveniently, and surprisingly to me, the regex processor accepts it either way. A two-character sequence "\t" matches a tab character, as does a three-character sequence "\\t". Please see demo code below. The system.out calls demonstrate which sequences and patterns, have tabs, and in JDK 1.7 both matches yield true.
Output on my JDK1.7 system is below, tabs in output might not survive SO formatter :)
HTH