java正则表达式的帮助

java正则表达式的帮助,java,regex,Java,Regex,嘿,我一直在和这个正则表达式斗争,我没有主意了。 我有这种类型的字符串(不是所有的都在这里,但只有这两种类型),我必须提取th标记之间的部分 <th class="tip" title='manje'>manje</th> <th class="tip" title='ne d.'>ne d.</th> <th class="tip" title='manje'>manje</th> <th class="tip" t

嘿,我一直在和这个正则表达式斗争,我没有主意了。 我有这种类型的字符串(不是所有的都在这里,但只有这两种类型),我必须提取th标记之间的部分

<th class="tip" title='manje'>manje</th>
<th class="tip" title='ne d.'>ne d.</th>
<th class="tip" title='manje'>manje</th>
<th class="tip" title='točno'>točno</th>
<th class="tip" title='više'>više</th>
<th class="tip" title='m./t.'>m./t.</th>
<th class="tip" title='v./t.'>v./t.</th>
<th class="tip">daje</th>
<th class="tip">X2</th>
<th class="tip">12</th>
manje
东北。
曼杰
托契诺
维什
m、 /t。
v、 /t。
大捷
X2
12
我尝试了一些组合,但只有在th标记中没有该属性“title”时,我才能得到该值

此模式仅在th标记中没有“title”属性时提取内容:

Pattern pattern = Pattern.compile("<th class=\"tip\"[\\s*|[.]{0,20}]>(.*?)\\s*</th>");
Pattern=Pattern.compile((**?\\s*);
这一条还包括:

Pattern patternType = Pattern.compile("<th class=\"tip\"[\\s*|[.]{0,20}]>(.*?)\\s*</th>");
patternType=Pattern.compile((*?\\s*);
有什么建议吗?Tnx

试试这个:

Pattern pattern = Pattern.compile("<th[^>]*>(.*?)\\s*</th>");
Pattern=Pattern.compile(“]*>(.*?\\s*)”;
试试这个:

Pattern pattern = Pattern.compile("<th class=\"tip\"[^>]*>(.*)</th>");
Pattern=Pattern.compile(“]*>(.*)”;

见鬼,再尝试一次模式回答,这一次有“向前看”和“向后看”:

Pattern pattern = Pattern.compile("(?<=<th .{0,100}>).*(?=</th>)");

这假设文本文件名为Foo1.txt,并且它与类文件位于一起。

正则表达式并不适用于所有情况。改用Jsoup:

package so6235727;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class PrintContent {

  private static final String html = //
      "<th class=\"tip\" title='manje'>manje</th>\r\n" + //
      "<th class=\"tip\" title='ne d.'>ne d.</th>\r\n" + //
      "<th class=\"tip\" title='manje'>manje</th>\r\n" + //
      "<th class=\"tip\" title='točno'>točno</th>\r\n" + //
      "<th class=\"tip\" title='više'>više</th>\r\n" + //
      "<th class=\"tip\" title='m./t.'>m./t.</th>\r\n" + //
      "<th class=\"tip\" title='v./t.'>v./t.</th>\r\n" + //
      "<th class=\"tip\">daje</th>\r\n" + //
      "<th class=\"tip\">X2</th>\r\n" + //
      "<th class=\"tip\">12</th>\r\n";

  public static void main(String[] args) {
    Document jsoup = Jsoup.parse(html);
    Elements headings = jsoup.select("th.tip");
    for (Element element : headings) {
      System.out.println(element.text());
    }
  }
}
包so6235727;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
导入org.jsoup.nodes.Element;
导入org.jsoup.select.Elements;
公共类打印内容{
私有静态最终字符串html=//
“manje\r\n”+//
“东北。\r\n”+//
“manje\r\n”+//
“točno\r\n”+//
“više\r\n”+//
“m./t.\r\n”+//
“v./t.\r\n”+//
“daje\r\n”+//
“X2\r\n”+//
“12\r\n”;
公共静态void main(字符串[]args){
文档jsoup=jsoup.parse(html);
元素标题=jsoup.select(“th.tip”);
用于(要素:标题){
System.out.println(element.text());
}
}
}

看到这有多容易了吗?

我之所以包括我的测试代码,是因为当其他人有负/正匹配时,我似乎有正/负匹配

 import java.util.regex.Matcher;
 import java.util.regex.Pattern;

public class Regex {

public static void test(String patternString) {
    System.out.println("Test with pattern: " + patternString);
    Pattern pattern = Pattern.compile(patternString);
    String[] testStrings = {"<th class=\"tip\" title='manje'>manje</th>", "<th class=\"tip\">daje</th>"};
    for (String testString : testStrings) {
        System.out.println("> Test on " + testString);
        Matcher matcher = pattern.matcher(testString);
        if (matcher.matches()) {
            System.out.println(">> number of matches in group = " + matcher.groupCount());
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println(">>group " + i + " is " + matcher.group(i));
            }
        } else {
            System.out.println(">> no match");
        }
    }
    System.out.println("");
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    test("<th class=\"tip\"[\\s*|[.]{0,20}]>(.*?)\\s*</th>"); // op
    test("<th[^>]*>(.*?)\\s*</th>"); // Billy Moon
    test("<th class=\"tip\"[^>]*>(.*)</th>"); // stuken.yuri
    test("(?<=<th .{0,100}>).*(?=</th>)"); // Hovercraft full of Eels
    test("(?:<th .{0,100}>).*(?:</th>)");
}
}
import java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类正则表达式{
公共静态无效测试(字符串模式字符串){
System.out.println(“使用模式测试:+patternString”);
Pattern=Pattern.compile(patternString);
String[]testStrings={“manje”,“daje”};
for(字符串testString:testStrings){
System.out.println(“>teston”+testString);
Matcher Matcher=pattern.Matcher(testString);
if(matcher.matches()){
System.out.println(“>>组中的匹配数=“+matcher.groupCount());

对于(int i=1;i哪一个有效?对使用“[]”的字符进行评分,它定义了一个要匹配的字符。我不确定它实际要匹配“[.”的字符。因为您正在阅读XML,所以可能更适合使用dom和sax实用程序。这些是Java SE基本包的一部分。奇怪的是……您的模式在我的机器上确实不起作用,而其他模式却起作用。(我删除了我的评论,说它不起作用。我的装备不同,但我看不出它对比赛有什么影响…)如果我更改你的前瞻性内容(“(?@toto:也许如果你编辑了你的原始帖子并向我们展示了你的测试工具,最好是作为一个小的可编译和可运行的程序,我可以更好地了解我的正则表达式为什么会失败。我把我的代码作为一个答案(很遗憾)。我现在必须运行,但我今晚会检查你是否有任何评论。我正在运行Java 7(测试版)在Windows 7上。Woohoo:D这一个对我有效。Tnx。匹配到“>”是个好主意。试过了,但对我无效。stuken。yuri的正则表达式有效。Tnx无论如何是的,但我有一个非常大的HTML页面,我必须解析,正则表达式在我的第一个提示处。但是这个工具看起来非常棒。我会试试。
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;

public class Regex {

public static void test(String patternString) {
    System.out.println("Test with pattern: " + patternString);
    Pattern pattern = Pattern.compile(patternString);
    String[] testStrings = {"<th class=\"tip\" title='manje'>manje</th>", "<th class=\"tip\">daje</th>"};
    for (String testString : testStrings) {
        System.out.println("> Test on " + testString);
        Matcher matcher = pattern.matcher(testString);
        if (matcher.matches()) {
            System.out.println(">> number of matches in group = " + matcher.groupCount());
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println(">>group " + i + " is " + matcher.group(i));
            }
        } else {
            System.out.println(">> no match");
        }
    }
    System.out.println("");
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    test("<th class=\"tip\"[\\s*|[.]{0,20}]>(.*?)\\s*</th>"); // op
    test("<th[^>]*>(.*?)\\s*</th>"); // Billy Moon
    test("<th class=\"tip\"[^>]*>(.*)</th>"); // stuken.yuri
    test("(?<=<th .{0,100}>).*(?=</th>)"); // Hovercraft full of Eels
    test("(?:<th .{0,100}>).*(?:</th>)");
}
}