多行正则表达式匹配器

发布于 2024-09-26 14:49:45 字数 520 浏览 8 评论 0原文

有输入文件,内容为:
XX00002200000
XX00003300000

regexp:

(.{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 中失败了。

There is input file with content:
XX00002200000
XX00003300000

regexp:

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

Tried in The Regex Coach(app for regexp testing), strings are matched OK.

Java:

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

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

patternString is regexp(mentioned above) added as bean property from .xml

It's failed from Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡莣 2024-10-03 14:49:45

简单的解决方案:".{6}22.{5}\\s+.{6}33.{5}"。请注意, \s+ 是后续空格的 简写元素。

下面是一个示例:

 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 Regex,您可以使用:正则表达式编辑器(免费在线编辑器)

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

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;

Simple solution: ".{6}22.{5}\\s+.{6}33.{5}". Note that \s+ is a shorthand for consequent whitespace elements.

Heres an example:

 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);
  }
 }

With output:

XX00002200000
XX00003300000

To play around with Java Regex you can use: Regular Expression Editor (free online editor)

Edit: I think that you are changing the input when you are reading data, try:

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();
}

Or

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();
}

With imports like:

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;
木有鱼丸 2024-10-03 14:49:45

尝试在分隔符中进行此更改:

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

另外,为什么不使用更通用的正则表达式,如下所示:

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

上面提到的正则表达式适用于 2 行。既然您提到分隔符作为新行,您应该给出合适的正则表达式对于单行。

Try this change in delimiter:

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

also why don't you use a more general regex expression like this :

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

The regex you have mentioned above is for 2 lines.Since you have mentioned the delimiter as a new line you should be giving a regex expression suitable for a single line.

﹎☆浅夏丿初晴 2024-10-03 14:49:45

请原谅我的无知,但我仍然不确定你到底想搜索什么。如果您尝试搜索字符串(带有新行)

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

Pardon my ignorance, but I am still not sure what exactly are you trying to search. In case, you are trying to search for the string (with new lines)

XX00002200000
XX00003300000

then why are you reading it by delimiting it by new lines?

To read the above string as it is, the following code works

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();
        }

NB: here new.txt file contains the string

XX00002200000
XX00003300000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文