java - 匹配书名的正则表达式——组

标签 java regex

我有一个我写的正则表达式:

value='[A-Za-z]+\\,[0-9]+\\,([A-Za-z0-9]+)\\,([A-Za-z0-9]+)'>[A-Za-z0-9]+\\s-\\s(.*)?\\s\\(

它工作得相当好,但问题是它的最后总是匹配所有东西..

例如,它应该适用于书籍,我正在以下方面对其进行测试:

value='C,201301,F110,JEWL1050'>JEWL1050 - Industry Skills I (F110)</option>
value='C,201301,F114,JEWL1050'>JEWL1050 - Industry Skills I (F114)</option>
value='C,201301,F114,JEWL1054'>JEWL1054 - Jewellery Rendering & Illustra (F114)</option>
value='C,201301,F110,JEWL2029'>JEWL2029 - Production Techniques B (F110)</option>
value='C,201301,F114,JEWL2029'>JEWL2029 - Production Techniques B (F114)</option>
value='C,201301,LIAD,LANG9066'>LANG9066 - Italian For Beginners (LIAD)</option>
value='C,201301,T302,LAW1151'>LAW1151 - Canandian & Environmental Law (T302)</option>
value='C,201301,T305,LAW1151'>LAW1151 - Canandian & Environmental Law (T305)</option>
value='C,201301,F402,LAW1152'>LAW1152 - International Law & Agreements (F402)</option>
value='C,201301,T302,LAW3201'>LAW3201 - Protection Legislation (T302)</option>
value='C,201301,T303,LAW3201'>LAW3201 - Protection Legislation (T303)</option>
value='C,201301,T304,LAW3201'>LAW3201 - Protection Legislation (T304)</option>

所以对于第一本书,它应该捕获 F110作为第 1 组,JEWL1050作为第 2 组,和 Industry Skills I作为第 3 组..

但是,它正确捕获了前两组,但没有捕获最后一组。它捕获 - Industry Skills I (F110)</option>相反..

有什么办法可以修复我的正则表达式吗?我似乎根本无法完成最后一组。 请帮我。先谢谢你。

最佳答案

理论上,这应该按原样工作。

这是您建议的正则表达式(由于工具的性质与 Java 代码相比,\\ 更改为 \)应用于示例输入时:http://regex101.com/r/hL8pZ8

此工具还提供了一个“Java”复选框,甚至还提供了相应的 Java 代码,尽管没有固定链接,因此您必须输入正则表达式(再次使用 \\ 而不是 \) 和自己的示例数据:http://www.myregextester.com/index.php

也就是说,为了后代,这里是它的输出:

Raw Match Pattern:

  value='[A-Za-z]+\,[0-9]+\,([A-Za-z0-9]+)\,([A-Za-z0-9]+)'>[A-Za-z0-9]+\s-\s(.*)?\s\(

Java Code Example:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
    String sourcestring = "source string to match with pattern";
    Pattern re = Pattern.compile("value='[A-Za-z]+\\,[0-9]+\\,([A-Za-z0-9]+)\\,([A-Za-z0-9]+)'>[A-Za-z0-9]+\\s-\\s(.*)?\\s\\(");
    Matcher m = re.matcher(sourcestring);
    int mIdx = 0;
    while (m.find()){
      for (int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++){
        System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
      }
      mIdx++;
    }
  }
}

$matches Array:
(
  [0] => Array
    (
      [0] => value='C,201301,F110,JEWL1050'>JEWL1050 - Industry Skills I (
      [1] => value='C,201301,F114,JEWL1050'>JEWL1050 - Industry Skills I (
      [2] => value='C,201301,F114,JEWL1054'>JEWL1054 - Jewellery Rendering & Illustra (
      [3] => value='C,201301,F110,JEWL2029'>JEWL2029 - Production Techniques B (
      [4] => value='C,201301,F114,JEWL2029'>JEWL2029 - Production Techniques B (
      [5] => value='C,201301,LIAD,LANG9066'>LANG9066 - Italian For Beginners (
      [6] => value='C,201301,T302,LAW1151'>LAW1151 - Canandian & Environmental Law (
      [7] => value='C,201301,T305,LAW1151'>LAW1151 - Canandian & Environmental Law (
      [8] => value='C,201301,F402,LAW1152'>LAW1152 - International Law & Agreements (
      [9] => value='C,201301,T302,LAW3201'>LAW3201 - Protection Legislation (
      [10] => value='C,201301,T303,LAW3201'>LAW3201 - Protection Legislation (
      [11] => value='C,201301,T304,LAW3201'>LAW3201 - Protection Legislation (
    )

  [1] => Array
    (
      [0] => F110
      [1] => F114
      [2] => F114
      [3] => F110
      [4] => F114
      [5] => LIAD
      [6] => T302
      [7] => T305
      [8] => F402
      [9] => T302
      [10] => T303
      [11] => T304
    )

  [2] => Array
    (
      [0] => JEWL1050
      [1] => JEWL1050
      [2] => JEWL1054
      [3] => JEWL2029
      [4] => JEWL2029
      [5] => LANG9066
      [6] => LAW1151
      [7] => LAW1151
      [8] => LAW1152
      [9] => LAW3201
      [10] => LAW3201
      [11] => LAW3201
    )

  [3] => Array
    (
      [0] => Industry Skills I
      [1] => Industry Skills I
      [2] => Jewellery Rendering & Illustra
      [3] => Production Techniques B
      [4] => Production Techniques B
      [5] => Italian For Beginners
      [6] => Canandian & Environmental Law
      [7] => Canandian & Environmental Law
      [8] => International Law & Agreements
      [9] => Protection Legislation
      [10] => Protection Legislation
      [11] => Protection Legislation
    )
)

关于java - 匹配书名的正则表达式——组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19442757/

相关文章:

java - 运行 Maven 测试时 Eclipse 中出现错误 : no lwjgl64 in java. library.path

Python 3 pandas 使用字符串与正则表达式标记数据框中的数据

java - 我无法运行 WiEngine 骨架项目

java - 理解Java方法声明中的<Req extends Request<Res>, Res extends Response>

java - 我收到错误 : "Overload resolution ambiguity" from MapElements transform in Apache Beam when using Kotlin

regex - sed 和正则表达式问题

regex - 找到匹配项后,Sed 内联替换文本文件中的句子

JavaScript 从字符串中删除尾随空格和句点

javascript - 用于验证十进制数的 JavaScript 中的正则表达式

java - String.replace 不起作用