使用 JTextField 从用户获取正则表达式。如何让它将 \t 视为制表符而不是 \ 后跟 at

发布于 2025-01-03 10:20:52 字数 823 浏览 1 评论 0原文

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 finds t 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩物 2025-01-10 10:20:52

示例:

String src = "This\tis\ta\ttest";
System.out.println("src=\"" + src + '"'); // --> prints "This is a test"
String re="\\t";
System.out.println("re=\"" + re + '"'); // --> prints "\t" - as when you use reSource.getText();

Pattern p = Pattern.compile(re);
Matcher m = p.matcher(src);
while (m.find()) {
    System.out.println('"' + m.group() + '"');
}

输出:

src="This   is  a   test"
re="\t"
"   "
"   "
"   "

试试这个:

re=re.replace("\\t", "\t");
OR
re=re.replace("\\t", "\\\\t");

我认为问题在于理解当您输入:

String str = "\t";

那么它实际上与:

String str = "   ";

但如果您输入:

String str = "\\t";

那么 System.out.print(str) 将是“\t”

Example:

String src = "This\tis\ta\ttest";
System.out.println("src=\"" + src + '"'); // --> prints "This is a test"
String re="\\t";
System.out.println("re=\"" + re + '"'); // --> prints "\t" - as when you use reSource.getText();

Pattern p = Pattern.compile(re);
Matcher m = p.matcher(src);
while (m.find()) {
    System.out.println('"' + m.group() + '"');
}

Output:

src="This   is  a   test"
re="\t"
"   "
"   "
"   "

Try this:

re=re.replace("\\t", "\t");
OR
re=re.replace("\\t", "\\\\t");

I think the problem is in understanding that when you type:

String str = "\t";

Then it is actualy same as:

String str = "   ";

But if you type:

String str = "\\t";

Then the System.out.print(str) will be "\t".

甜柠檬 2025-01-10 10:20:52

匹配 \t 应该可以工作,但是,您的标志可能有问题。

这对我有用:

String src = "A\tBC\tD";
Pattern p=Pattern.compile("\\w\\t\\w"); //simulates the user entering \w\t\w
Matcher m=p.matcher(src);
while (m.find()) 
{
  System.out.println("Match: \"" + m.group(0) + "\"");
}

输出是:

Match: "A   B"
Match: "C   D"

Matching \t should work, however, your flags might have a problem.

Here's what works for me:

String src = "A\tBC\tD";
Pattern p=Pattern.compile("\\w\\t\\w"); //simulates the user entering \w\t\w
Matcher m=p.matcher(src);
while (m.find()) 
{
  System.out.println("Match: \"" + m.group(0) + "\"");
}

Output is:

Match: "A   B"
Match: "C   D"
淡墨 2025-01-10 10:20:52

我的经验是,Java Swing JTextField 和 JTable GUI 控件通过添加反斜杠前缀来转义用户输入的反斜杠。

用户输入两个字符序列“反斜杠t”,控件的getText()方法返回一个包含三个字符序列“反斜杠反斜杠t”的字符串。 SO 格式化程序对文本中的反斜杠执行自己的操作,因此这里是代码:

  Single backslash: input is 2 char sequence \t and return value is 3 char \\t

对于三字符输入序列“backsl backsl t”,getText() 返回五字符序列“backsl backsl backsl backsl t”。作为代码:

  Double backslash: input is 3 char sequence \\t and return value is 5 char \\\\t

这基本上可以防止反斜杠修改 t 以产生在由 System.out.println 等解释时变成制表符的字符序列。

方便且令我惊讶的是,正则表达式处理器无论如何都接受它。两个字符序列“\t”与制表符匹配,三个字符序列“\\t”也是如此。请参阅下面的演示代码。 system.out 调用演示了哪些序列和模式具有制表符,并且在 JDK 1.7 中,两者匹配都会产生 true。

package my.text;

/**
 * Demonstrate use of tab character in regexes
 */
public class RegexForSo {
public static void main(String [] argv) {
    final String sequenceTab="x\ty\tz";
    final String patternBsTab = "x\t.*";
    final String patternBsBsTab = "x\\t.*";
    System.out.println("sequence is >" + sequenceTab + "<");
    System.out.println("pattern BsTab is >" + patternBsTab + "<");
    System.out.println("pattern BsBsTab is >" + patternBsBsTab + "<");
    System.out.println("matched BsTab = " + sequenceTab.matches(patternBsTab));
    System.out.println("matched BsBsTab = " + sequenceTab.matches(patternBsBsTab));     
  }
}

我的 JDK1.7 系统上的输出如下,输出中的选项卡可能无法在 SO 格式化程序中保存:)

sequence is >x  y   z<
pattern BsTab is >x .*<
pattern BsBsTab is >x\t.*<
matched BsTab = true
matched BsBsTab = true

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:

  Single backslash: input is 2 char sequence \t and return value is 3 char \\t

For three-character input sequence "backsl backsl t", getText() returns the five-character sequence "backsl backsl backsl backsl t". As code:

  Double backslash: input is 3 char sequence \\t and return value is 5 char \\\\t

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.

package my.text;

/**
 * Demonstrate use of tab character in regexes
 */
public class RegexForSo {
public static void main(String [] argv) {
    final String sequenceTab="x\ty\tz";
    final String patternBsTab = "x\t.*";
    final String patternBsBsTab = "x\\t.*";
    System.out.println("sequence is >" + sequenceTab + "<");
    System.out.println("pattern BsTab is >" + patternBsTab + "<");
    System.out.println("pattern BsBsTab is >" + patternBsBsTab + "<");
    System.out.println("matched BsTab = " + sequenceTab.matches(patternBsTab));
    System.out.println("matched BsBsTab = " + sequenceTab.matches(patternBsBsTab));     
  }
}

Output on my JDK1.7 system is below, tabs in output might not survive SO formatter :)

sequence is >x  y   z<
pattern BsTab is >x .*<
pattern BsBsTab is >x\t.*<
matched BsTab = true
matched BsBsTab = true

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文