菜鸟求教,我JAVA中正则表达式无法验证从TXT文档读取的数据?
先做的一个简单的QQ验证,正则表达式[1-9]\\d{4,9},应该不错。
而且也考虑了去除可能存在的格式符
\b|\t|\n|\f|\r
文本文档是ANSI码的,换成其他编码之后一样无法验证。。。。
本菜鸟先在谢过各位大侠!!
这个是生成界面以及读取文件的代码。(其中save按钮尚未完成)
public class UI2 implements ActionListener{
JLabel inputLabel;
JLabel outputLabel;
JTextArea input;
JTextArea output;
JPanel p0;
JPanel p1;
JPanel p2;
JPanel p3;
JButton ok;
JButton open;
JButton save;
private JFrame frm;
public static File f;
private JFileChooser fc;
private int flag;
public Data Data;
public List<Data> list=null ;
public Data1 Data1 = null;
public List<Data1> list1 = new ArrayList<Data1>();
UI2(){
frm=new JFrame("特征词验证");
Container c=frm.getContentPane();
frm.setBounds(0,0,400,400);
fc=new JFileChooser();
inputLabel=new JLabel("输入");
outputLabel=new JLabel("输出");
input=new JTextArea();
output=new JTextArea();
output.setEditable(false);
p0=new JPanel(new GridLayout(2,1));
p1=new JPanel(new GridLayout(1,2));
p2=new JPanel(new GridLayout(1,2));
p3=new JPanel(new GridLayout(1,2,10,10));
ok=new JButton("验证");
open=new JButton("打开");
save=new JButton("保存");
ok.addActionListener(new action());
open.addActionListener(new action());
save.addActionListener(new action());
p1.add(open);
p1.add(ok);
p1.add(save);
p2.add(inputLabel);
p2.add(outputLabel);
p0.add(p1);
p0.add(p2);
p3.add(input);
p3.add(output);
c.add(p0,BorderLayout.NORTH);
c.add(p3,BorderLayout.CENTER);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(frm);
frm.validate();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
public File getFile(){
return f;
}
private void openFile() throws IOException //打开文件
{
SwingUtilities.updateComponentTreeUI(fc);
//设置打开文件对话框的标题
fc.setDialogTitle("打开文件");
//这里显示打开文件的对话框
try{
flag=fc.showOpenDialog(frm);
}
catch(HeadlessException head){
System.out.println("Open File Dialog ERROR!");
}
//如果按下确定按钮,则获得该文件。
if(flag==JFileChooser.APPROVE_OPTION)
{
//获得该文件
f=fc.getSelectedFile();
}
list = readtxt(f);
input.append("文件地址:"+ f.getPath()+"\n" );
for (int i = 0; i < list.size(); i++) {
Data = (Data) list.get(i);
input.append(Data.getdata()+"\n");
}
}
private List<Data> readtxt(File f2) {
// TODO Auto-generated method stub
Data = null;
List<Data> list = new ArrayList<Data>();
File file = new File(f.getAbsolutePath());
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null){
line ++;
Data = new Data();
Data.setdata(tempString);
list.add(Data);
}
reader.close();
} catch (IOException e){
e.printStackTrace();
}finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1){
}
}
}
return list;
}
public void regexFile(){
for (int i = 0; i < list.size(); i++) {
Data = (Data) list.get(i);
Data1 = new Data1();
Data1 = Regex1.regex1(Data.getdata());
list1.add(Data1);
}
output.append("待验证内容 QQ \n");
for (int i = 0; i < list1.size()-1; i++) {
Data1 = (Data1) list1.get(i);
output.append(Data1.getdata()+" "+Data1.getIsqq()+"\n");
}
}
//按钮易做图类内部类
class action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//判断是哪个按纽被点击了
if(e.getSource()==open){
try {
openFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
if(e.getSource()==ok)
regexFile();
}
}
public static void main(String[] args){
new UI2();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
这个是类Data的代码,用于存放读取的数据
public class Data {
private String data;
public String getdata() {
return data;
}
public void setdata(String data) {
this.data = data;
}
}
这个是Data1的代码,用于存放验证后的内容。
public class Data1 {
private String data;
private boolean Isqq;
public String getdata() {
return data;
}
public void setdata(String data) {
this.data = data;
}
public String getIsqq(){
if(Isqq == true)
return "true";
else
return "false";
}
public void setIsqq(boolean getIsqq){
this.Isqq = Isqq;
}
}
这个是用于验证QQ号码的类,已经考虑了去除一些格式控制符
--------------------编程问答-------------------- --------------------编程问答-------------------- ==难道我玩的单机版。。。。 --------------------编程问答-------------------- 说真的是没看懂你要干嘛?就是验证字符串嘛,那位看你的正则也没啥问题啊?如果你考虑到\\s,那么你先replaceAll("\\s","");处理一下就可以了啊
public class Regex1 {
public static Data1 regex1(String a){
Regex1 regex=new Regex1();
Data1 Data;
Data = new Data1();
//UI2.;
Pattern p = Pattern.compile("\\s*|\b|\t|\n|\f|\r");
Matcher m = p.matcher(a);
a = m.replaceAll("");
Data.setdata(a);
Data.setIsqq(regex.qq(a));
return Data;
}
public boolean qq(String a){
Pattern p=null;
Matcher m=null;
boolean b=false;
p=Pattern.compile("[1-9]\\d{4,9}");
m=p.matcher(a);
b=m.matches();
return b;
}
}
补充:Java , Eclipse