正则表达式 - 在可选引号之间

时间:2011-12-24 05:09:39

标签: regex perl unix scripting character-encoding

要搜索的字符串示例:

name = 'bob'
person=mary
init= 'tim', 
first =sarah,
,name=o'donnel
surny = 'o'doherty',
extend = 'marshall , mathers (111)'

下面的正则表达式搜索是错误的:

[ ]*,?[ ]*(.+)[ ]*=[ ]*'?(.*)'?[ ]*(,|;)*[ ]*

正则表达式替换为2组:\1 \2

以下所需结果:

name bob
person mary
init tim 
first sarah
name o'donnel
surny o'doherty
extended marshall , mathers (111)

伪代码基本上是:

optional spaces,one optional comma,optional spaces,any characters (grouped),optional spaces,optional spaces,one optional single quote,any characters (grouped),one optional single quote,optional spaces,one optional comma or semicolon,optional spaces

1 个答案:

答案 0 :(得分:4)

让它变得非贪婪:(.+) - > (.+?)(.*) - > (.*?)demo