正则表达式获取键值,包括空白空间

时间:2013-01-29 06:36:01

标签: java php regex perl expression

我有一个问题,就是从我的正则表达式中获取一个空格。 任何人都可以帮助我使用键值机制提供正确的正则表达式。请注意,键值位置可能会更改,但不会修复。

正则表达式

\s*([\w\d_]*)=(.*?)\s+

输入

spt=38271 deviceExternalId=SUKUK node 1 dst=99.111.185.141 app=HTTPS rt=Jan 23 2013 15:30:45 deviceFacility=Packet filter msg=Connection was reset by server destinationTranslatedPort=443 

我的结果

Match 1: spt=38271 
    Subgroups:
    1: spt
    2: 38271
Match 2: deviceExternalId=SUKUK 
    Subgroups:
    1: deviceExternalId
    2: SUKUK
Match 3:  dst=99.111.185.141 
    Subgroups:
    1: dst
    2: 99.111.185.141
Match 4: app=HTTPS 
    Subgroups:
    1: app
    2: HTTPS
Match 5: rt=Jan 
    Subgroups:
    1: rt
    2: Jan          **==>Should grab [Jan 23 2013 15:30:45]**
Match 6:  deviceFacility=Packet 
    Subgroups:
    1: deviceFacility
    2: Packet       **==>Should grab [Packet filter]**
Match 7:  msg=Connection 
    Subgroups:
    1: msg
    2: Connection   **==>Should grab [Connection was reset by server]**
Match 8:  destinationTranslatedPort=443 
    Subgroups:
    1: destinationTranslatedPort
    2: 443

2 个答案:

答案 0 :(得分:2)

试试这个:

([\w\d_]+)=(.*?)(?=\s+[\w\d_]+=|$)

print "$1\n$2";

答案 1 :(得分:1)

更简单:\s*([\w\d_]*)=([^=]+)\s+