正则表达式grep多行数据

时间:2013-04-10 07:39:34

标签: java regex

我有一个大文本数据,从中我能够达到特定部分。具体部分如下所示:

Caption = "Universal Plug and Play Device Host"
   CheckPoint = 0
   CreationClassName = "Win32_Service"
   Description = "Provides support to host Universal Plug and Play devices."
   DesktopInteract = FALSE
   DisplayName = "Universal Plug and Play Device Host"
   ErrorControl = "Normal"
   ExitCode = 1077
   Name = "upnphost"
   PathName = "C:\\WINDOWS\\system32\\svchost.exe -k LocalService"
   ProcessId = 0
   ServiceSpecificExitCode = 0
   ServiceType = "Share Process"
   Started = FALSE
   StartMode = "Disabled"
   StartName = "NT AUTHORITY\\LocalService"
   State = "Stopped"
   Status = "OK"
   SystemCreationClassName = "Win32_ComputerSystem"
   SystemName = "KYAKKALA-WXP"
   TagId = 0
   WaitHint = 0

我需要将文本和商店分开。

我尝试使用以下正则表达式:

String REGEX ="(Caption)\\s=.*?(VMware USB.*)\"\\;\\n((?:(\\w+)\\s+=\\s+(.*)\\n)   {1,21}?)";

通过应用正则表达式,我进入gp1“caption”,gp2“vmware usb仲裁服务”,gp3“waithint”和gp4“0”。 我需要获取21行的所有数据 但它只取得第一行和最后一行。

2 个答案:

答案 0 :(得分:0)

您无法将任意数量的组与一个正则表达式匹配 您应该放弃多行匹配,并使用全局修饰符在每行上使用正则表达式。然后你可以遍历结果。

......或者像Deepak Bala所说并使用属性一样。

答案 1 :(得分:0)

似乎在*+{...}语句中覆盖了组号。因此,在执行(?:(...))*之类的操作时,每个新匹配都会覆盖组1,因此,在打印组1时,您只会看到最后一个匹配。

但是你可以这样做:(根据你的需要改变)

String str = "Caption = \"Universal Plug and Play Device Host\"\n"+
  "  CheckPoint = 0\n"+
  "  CreationClassName = \"Win32_Service\"\n"+
  "  Description = \"Provides support to host Universal Plug and Play devices\"";

String regex = "(?:^|\n)\\s*(\\w*)\\s*=\\s*(.*?)(?=\r?\n|$)";

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
   System.out.println("Name = " + m.group(1));
   System.out.println("Value = " + m.group(2));
}

将打印:

Name = Caption
Value = "Universal Plug and Play Device Host"
Name = CheckPoint
Value = 0
Name = CreationClassName
Value = "Win32_Service"
Name = Description
Value = "Provides support to host Universal Plug and Play devices"