2014-01-09 2 views
0

일치하지 않는 나는 다음과 같은 HTML이 있습니다정규식은 와일드 카드

<tr><td><font color="#306eff">P: </font>9283-1000<font color="#306eff">&nbsp;&nbsp; 

또는 내가 regexpal.com에 가서 다음과 같은 정규식 입력

<tr><td><font color="#306eff">P: </font>9283-1000 

<font color="#306eff">&nbsp;&nbsp; 

(줄 바꿈) :

P: </font>(.*?)<font 

을 그리고 그것은 일치합니다. 나는 자바에서 그것을 할 때, 그것은 일치하지 않습니다

Pattern rP = Pattern.compile(">P: </font>(.*?)<font"); 
    Matcher mP = rP.matcher(data); 

    if (mP.find()) { 
     System.out.println(mP.group(1).trim()); 
    } 

내가 다른 경우에 시도 여러 정규 표현식에이 있고 그들은 단순히 자바에서 작동하지 않습니다. 어떤 제안? 감사!

+0

자바는 자체 REGEX입니다 – Keerthivasan

+0

[대신 HTML 구문 분석기 사용] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?answertab=oldest # tab-top) – Reimeus

답변

1

기본적으로 도트는 개행과 일치하지 않습니다.

사용 Pattern rP = Pattern.compile(">P: </font>(.*?)<font", Pattern.DOTALL);

참조 here.

+0

정말 고마워, 내 문제를 해결! – user2320462

2

나를 위해 잘 작동합니다.

public static void main(String[] args) { 
     String data = "<tr><td><font color=\"#306eff\">P: </font>9283-1000<font color=\"#306eff\">&nbsp;&nbsp;"; 
     Pattern rP = Pattern.compile(">P: </font>(.*?)<font"); 
     Matcher mP = rP.matcher(data); 

     if (mP.find()) { 
      System.out.println(mP.group(1).trim()); 
     } 
    } 

이 인쇄물은 9283-1000입니다.

문제는 data이 프로그램에 입력 된 방법에있을 것 같습니다.
이 출력에서 ​​볼 수 있듯이 코드 자체는 정상이므로

+0

코드를 업데이트했습니다. 입력 내용에 개행이 있습니다. 그것이 일치하지 않는 이유입니다. 그게 어떻게 일치하는지 말해 줄 수 있어요? – user2320462

0

대신이 정규식을 시도해보십시오

(?ims).*?>P: </font>(.*?)<font.+ 

샘플 코드

public static void main(String[] args) { 
    String data="<tr><td><font color=\"#306eff\">P: </font>9283-1000<font color=\"#306eff\">&nbsp;&nbsp;"; 
    Pattern rP = Pattern.compile("(?ims).*?>P: </font>(.*?)<font.+"); 
    Matcher mP = rP.matcher(data); 

    if (mP.find()) { 
      System.out.println(mP.group(1).trim()); 
    } 
} 

출력

9283-1000

+0

작동하지 않습니다 .. 다른 팁? – user2320462

+0

@ user2320462 질문에 사용중인 데이터를 게시 할 수 있습니까? – Stephan

+0

내 질문이 업데이트되었습니다. 입력 내용에 개행 문자가 포함되어 있습니다. 제발 어떻게 대처할 수 있는지 말해 줄 수 있어요? Thanks – user2320462

0

이 시도 :

자바에서
String data="<tr><td><font color=\"#306eff\">P: </font>9283-1000<font color=\"#306eff\">&nbsp;&nbsp;"; 
Pattern rP = Pattern.compile(">P: </font>(.*?)<font"); 
Matcher mP = rP.matcher(data); 

if (mP.find()) { 
     System.out.println(mP.group(1).trim()); 
} 

유일한 차이는 이스케이프 문자입니다.

+0

죄송합니다, 그 차이가 보이지 않습니다 – user2320462

+0

차이가 데이터 변수에 있습니다. Java 문자열 문자는 ""사이에 있어야합니다. html 코드에는 큰 따옴표가 있습니다.따라서 이스케이프 문자 \를 사용하여 해당 문자를 이스케이프하고 싶습니다. –