我有一个用于Web Crowler的java文件。这给了我错误,比如行上的多个标记和一些行号。
我刚学会了模式和匹配器。所以我无法纠正错误。
我的代码是
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.awt.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
class initComponents extends Frame implements ActionListener {
// TextField addt ,factorialt,concatt1,concatt2,lengtht,palindromet,comparet1,comparet2,substringt1,substringt2;
//Button add1,factorial1,concat1,length1,palindrome1,compare1,substring1;
String s1="";
//String s3="";
//String s4="";
//String s2="";
Button Search,Clear;
TextField text1;
TextArea text2;
Label lab,lab1,lab2;
public initComponents()
{
MyWindowAdapter1 m=new MyWindowAdapter1(this);
addWindowListener(m);
setTitle("Web Crawler");
setLayout(null);
setSize(1000,1000);
setVisible(true);
setBackground(Color.LIGHT_GRAY);
lab=new Label("Name: ");
text1=new TextField(20);
//text2=new TextArea(80,80);
text2=new TextArea();
//lab1=new Label("Count");
//lab2=new Label();
Search=new Button("Search");
Clear=new Button("Clear");
lab.setBounds(50,50,80,20);
//lab1.setBounds(500,50,80,20);
//lab2.setBounds(590,50,250,20);
text1.setBounds(140,50,250,20);
Search.setBounds(50,100,80,20);
Clear.setBounds(150,100,80,20);
text2.setBounds(50, 200, 700, 500);
//text2.setBounds(200,100,80,20);
add(lab);
add(text1);
//add(lab1);
//add(lab2);
add(Search);
add(Clear);
add(text2);
setVisible(true);
setSize(800,800);
Search.addActionListener(this);
Clear.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
// TODO Auto-generated method stub
if(a.getSource().equals(Clear))
{
text1.setText("");
text2.setText("");
//text2.append("");
}
if(a.getSource().equals(Search))
{
s1=text1.getText();
text2.setText("");
//text2.append();
//web_crawler c=new web_crawler();
//c.crawling(s1);
try{
InputStreamReader in = new InputStreamReader(new URL(s1).openStream());
StringBuilder input = new StringBuilder();
int ch;
while ((ch = in.read()) != -1)
input.append((char) ch);
String patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>])\\s*>";
String abc=((?<html>(href|src)\s*=\s*")|(?<css>url\())(?<url>.*?)(?(html)"|\)); **// here is error**
Pattern pattern = Pattern.compile(abc, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
int count=0;
String abc="";
System.out.println(pattern);
System.out.println(matcher.toString());
while (matcher.find())
{
int start = matcher.start();
int end = matcher.end();
String match = input.substring(start, end);
//match.matches();
//jTextArea1.append(match);
//jTextArea1.append("\n");
//String[] tokens=match.split(" ");
//System.out.println(tokens[0].toString());
text2.append(match);
text2.append("\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (PatternSyntaxException e)
{
e.printStackTrace();
}
}
}
}
class MyWindowAdapter1 extends WindowAdapter{
initComponents mf;
public MyWindowAdapter1(initComponents mf){
this.mf=mf;
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
它会出现像
这样的错误 Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
Syntax error on token "?", new expected
Syntax error on token ">", Type expected after this token
Syntax error on token "Invalid Character", invalid AssignmentOperator
Syntax error on token "Invalid Character", delete this token
Syntax error on tokens, Expression expected instead
at initComponents.actionPerformed(initComponents.java:115)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
请帮帮我。感谢
答案 0 :(得分:3)
您需要在文字周围添加引号并添加正确的转义序列。你也两次声明String abc:
String abc=((?<html>(href|src)\s*=\s*")|(?<css>url\())(?<url>.*?)(?(html)"|\)); **// here is error**
Pattern pattern = Pattern.compile(abc, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
int count=0;
String abc=""; **redeclaration here**
将其更改为:
String abc="((?<html>(href|src)\\s*=\\s*\")|(?<css>url\\())(?<url>.*?)(?(html)\"|\\))";
Pattern pattern = Pattern.compile(abc, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
int count=0;
abc="";
答案 1 :(得分:0)
String abc="((?<html>(href|src)\\s*=\\s*\")|(?<css>url\\())(?<url>.*?)(?(html)\"|\\))";
// **here should be no more error**
问题是你忘了你的引号,你应该转义字符串中的反斜杠和引号。