【问题标题】:Multiline regexp matcher多行正则表达式匹配器
【发布时间】:2010-10-11 12:31:19
【问题描述】:

有输入文件的内容:
XX00002200000
XX00003300000

正则表达式:

(.{6}22.{5}\W)(.{6}33.{5})

在 The Regex Coach(用于正则表达式测试的应用程序)中尝试过,字符串匹配正常。

Java:

        pattern = Pattern.compile(patternString);
        inputStream = resource.getInputStream();

        scanner = new Scanner(inputStream, charsetName);
        scanner.useDelimiter("\r\n");

patternString 是作为 .xml 中的 bean 属性添加的正则表达式(如上所述)

Java 失败了。

【问题讨论】:

  • 什么是patternString?你给它分配了什么?你是怎么分配的?您确定您的反斜杠是正则表达式中的文字反斜杠吗?
  • 更不用说,你实际在哪里使用模式?
  • @Pace 我使用:String val =scanner.next(pattern);

标签: java regex multiline


【解决方案1】:

简单的解决方案:".{6}22.{5}\\s+.{6}33.{5}"。请注意,\s+ 是一个 shorthand,用于后续的空白元素。

这是一个例子:

 public static void main(String[] argv) throws FileNotFoundException {
  String input = "yXX00002200000\r\nXX00003300000\nshort", regex = ".{6}22.{5}\\s+.{6}33.{5}", result = "";
  Pattern pattern = Pattern.compile(regex);
  Matcher m = pattern.matcher(input);

  while (m.find()) {
   result = m.group();
   System.out.println(result);
  }
 }

有输出:

XX00002200000
XX00003300000

要玩转 Java 正则表达式,您可以使用:Regular Expression Editor(免费在线编辑器)

编辑:我认为您在读取数据时正在更改输入,请尝试:

public static String readFile(String filename) throws FileNotFoundException {
    Scanner sc = new Scanner(new File(filename));

    StringBuilder sb = new StringBuilder();
    while (sc.hasNextLine())
        sb.append(sc.nextLine());
    sc.close();

    return sb.toString();
}

或者

static String readFile(String path) {
    FileInputStream stream = null;
    FileChannel channel = null;
    MappedByteBuffer buffer = null;

    try {
        stream = new FileInputStream(new File(path));
        channel = stream.getChannel();
        buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0,
                channel.size());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            stream.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    return Charset.defaultCharset().decode(buffer).toString();
}

导入如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

【讨论】:

  • 嗨,Margus。有趣的是,我在“正则表达式教练”应用程序中尝试了您提出的正则表达式:.{6}22.{5}\\s+.{6}33.{5}。它工作正常(没有一个反斜杠)。 from Java id 不起作用,很奇怪。
  • 我应该以某种方式将 InputStrem 转换为 FileInputStream 以便调用 getChannel() 方法吗?
【解决方案2】:

尝试更改分隔符:

 scanner.useDelimiter("\\s+");

你为什么不使用更通用的正则表达式:

 ".{6}[0-9]{2}.{5}"

您上面提到的正则表达式是 2 行。由于您将分隔符作为新行提到,您应该提供适合单行的正则表达式。

【讨论】:

  • 谢谢你的回答,这个正则表达式需要从消息队列中提取明确的字符串缓冲区,这个字符串缓冲区从 22 的字符串开始,以 33 的字符串结束。实际上,这些字符串之间将是类似的结构,也用LR或LF分隔。
  • 我不太明白。我的回答对您有帮助吗?如果没有,请通过编辑您的问题详细解释上述问题。
【解决方案3】:

请原谅我的无知,但我仍然不确定您到底要搜索什么。以防万一,您正在尝试搜索字符串(带有新行)

XX00002200000
XX00003300000

那你为什么要通过换行来阅读它?

要按原样读取上面的字符串,下面的代码可以工作

Pattern p = Pattern.compile(".{6}22.{5}\\W+.{6}33.{5}");

 FileInputStream scanner = null;
        try {
            scanner = new FileInputStream("C:\\new.txt");
            {
                byte[] f = new byte[100];
                scanner.read(f);
                String s = new String(f);
                Matcher m = p.matcher(s);
                if(m.find())
                    System.out.println(m.group());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

注意:这里 new.txt 文件包含字符串

XX00002200000
XX00003300000

【讨论】:

  • 如何在 InputStream 中使用扫描仪?在scanner = new Scanner(inputStream, charsetName)的情况下,不支持方法读取
  • 我不知道为什么你有必要使用扫描仪从文件中读取,但如果是这样,那么最好使用文件中找不到的分隔符,例如scanner.useDelimiter("\\?");它将提示扫描仪从文件中获取整个字符串
猜你喜欢
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 2022-07-08
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多