2016-09-23 14 views
0

我想验证java文本区域中的选项卡,因为输入保存在按标签拆分的文本文件中。如果我键入标签,它将不会读取下一个输入。我试过了,例如:String.matches(“\ t”),但它不起作用。 谢谢。如何验证jText区域中的选项卡?

这里是我的代码:

String Title = textfield.getText(); 
String Description = textarea.getText(); 

if (Description.matches("\t")) 
{ 
    JOptionPane.showMessageDialog(this, "Tab is not allowed"); 
} 
else 
{ 
    try 
    { 
     File f = new File("Data.txt"); 

     try (BufferedWriter writer = new BufferedWriter (new FileWriter(f,true))) 
     {   
      writer.write(Title+"\t"+Description+System.lineSeparator());   
     } 
    } 
    catch(IOException e) 
    { 

    } 
} 

回答

0

您是否在寻找一个制表符或较大字符串中制表符?如果是后者请尝试:

Description.matches(".*\t.*") 
+0

它工作!非常感谢!你能解释这些代码吗? – Pepper

+0

该点匹配任何字符,*表示“多次前面的表达(点)”。所以这个正则表达式的意思是:“任意数量的字符(零个或多个),后跟一个制表符,后跟任意数量的字符”。 –

+0

很好的解释。谢谢! – Pepper

相关问题