繁体   English   中英

正则表达式,用于捕获特定数字

[英]RegEx for capturing particular digits

从下面的日志中,我如何单独grep'951792'值

2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes
2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e
2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   

我尝试了Java拆分/子字符串操作。 但是代码行很高。 使用正则表达式如何获取“ 951792 ”值

输出将是

951792
951895
951934 
075041

在这里,我们可能想要简单地使用右边界[mod靠近我们想要的数字,并在我们的第一个捕获组中收集数字,也许与此类似:

([0-9]+)\s\[m 

如果我们愿意,我们可以添加更多边界,例如:

(.+?)([0-9]+)\s\[m.+

在此处输入图片说明

演示

测试

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

final String regex = "(.+?)([0-9]+)\\s\\[m.+";
final String string = "2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes\n"
     + "2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e\n"
     + "2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   \n";
final String subst = "\\2";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);

System.out.println("Substitution result: " + result);

演示版

 const regex = /(.+?)([0-9]+)\\s\\[m.+/gm; const str = `2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes 2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e 2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10 `; const subst = `$2`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log('Substitution result: ', result); 

正则表达式

如果不需要此表达式,则可以在regex101.com中对其进行修改或更改。

RegEx电路

jex.im可视化正则表达式:

在此处输入图片说明

您可以尝试以下正则表达式:

(?<=[0-9]{6}-[0-9]{2}:[0-9]{2}:[0-9]{2}\.)[0-9]+

别忘了两次逃脱. \\\\. ),将其添加到您的Java代码中。

输入:

2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes
2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e
2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10 

火柴:

951792
951895
951934 

演示1

对于使用前瞻性和后瞻性的更严格的正则表达式,请使用:

(?<=[0-9]\]:\s[0-9]{6}-[0-9]{2}:[0-9]{2}:[0-9]{2}\.)[0-9]+(?=\s\[mod=REC)

演示2

Java代码示例:

String input = "2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes\n" + 
                "2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e\n" + 
                "2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   ";
List<String> matches = new ArrayList<String>();
Matcher m = Pattern.compile("(?<=[0-9]{6}-[0-9]{2}:[0-9]{2}:[0-9]{2}\\.)[0-9]+")
.matcher(input);
while (m.find()) {
    matches.add(m.group());
}
System.out.println(matches);

代码输出:

[951792, 951895, 951934]

//逐行迭代循环。

String line = "2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes";
      String pattern = "^.+\\.(\\d+)";

      // Create a Pattern object
      Pattern r = Pattern.compile(pattern);

      // Now create matcher object.
      Matcher m = r.matcher(line);
      if (m.find( )) {
          System.out.println("Found value: " + m.group(1) ); //This would give 951792
              }else {
         System.out.println("NO MATCH");
      }

在此处获取正则表达式参考: https : //regex101.com/r/8F0D4w/1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM