你好,这是我的文本内容
data1='res1', data2='res2', data3 = 'anything like data5='', get complete', data4 = 'anything'
我想使用php正则表达式来获取所有(数据)和(数据值),但存在问题。我的问题是data3
值(anything like data5='', get complete)
内容data5=''
和data 5
是值而不是数据密钥
在我的程序中,data5
检测为单独的键,而这只是data3
的值,必须在我的正则表达式中的所有值的数组中检测
我可以使用什么regex来解决此问题,并且regex读取data1
、data2
、data3
、data4
与(rest1
、res2
,(任何类似data5=''
、get complete
的内容),任何内容)分离?
如果数据结构是固定的,你可以使用这样的东西:
$re = "~ ([^W_]+) h*=h* '(.*?)' h*,h*
([^W_]+) h*=h* '(.*?)' h*,h*
([^W_]+) h*=h* '(.*?)' h*,h*
([^W_]+) h*=h* '(.*?)' ~x";
$subst = " $1 -> $2n $3 -> $4n $5 -> $6n $7 -> $8n";
$data = "data1='res1', data2='res2', data3 = 'anything like data5='', get complete', data4 = 'anything'nndata1='res1', data2='res2', data3 = 'anything like data5=''', data4 = 'anything'n# Odds single quotendata1=''res1', data2='res2', data3 = 'anything like data5=''', data4 = 'anything'n# Even quotes inside data1ndata1='''res1', data2='res2', data3 = 'anything like data5=''', data4 = 'anything'";
$result = preg_replace($re, $subst, $data);
print_r($result);
输入
data1="s1",data2="s2",data3="任何类似data5="的内容,完成",data4="任何内容"
输出/emspIdeone演示
data1->res1
data2->res2
data3->任何类似data5=''的内容,请完成
data4->任何
Regex突破/emspRegex101演示
所有4个部分
~ # regex start delimiter
([^W_]+) h*=h* '(.*?)' h*,h* # data1
([^W_]+) h*=h* '(.*?)' h*,h* # data2
([^W_]+) h*=h* '(.*?)' h*,h* # data3
([^W_]+) h*=h* '(.*?)' # data4
~x # close delimiter with 'x' freespace flag
单个数据段的详细信息(例如data1)
([^W_]+) # 1 or more alphanumeric chars (equal to [a-zA-Z0-9])
# the round brackets save them in $1 group
h*=h* # zero or more horizontal whitespace chars then a literal '='
# followed by zero or more whitespace chars again
'(.*?)' # a single quote "'", zero o more chars of any type, closing quote "'"
# the lazy modifiers '?' makes it stop at the first single quote
# that satisfy also the rest of the regex(!)
h*,h* # zero or more horizontal whitespace chars then a literal comma ','
# followed by zero or more whitespace chars again