[英]How can I write a regular expression that matches everything between the first and the last quote?
I try to match multiple values between quotes (these values can be anything but spaces) the best I can achieve is to match everything between the first and the last quote 我尝试匹配引号之间的多个值(这些值可以是空格,但不能是其他任何值),我能达到的最佳效果是匹配第一个和最后一个引号之间的所有内容
I already checked many SO answers, yet I cannot make it work 我已经检查了很多SO答案,但无法使其正常工作
here is the regex 这是正则表达式
\[\[\[(\w*img\w*)\s(\w*id|url\w*)+="([^"]|.*)"\]\]\]
here is the string I try to match (values are numbers but I could have urls or anything similar) 这是我尝试匹配的字符串(值是数字,但是我可以有url或类似的东西)
[[[img id="37" w="100" h="70"]]]
I should get all parameters and their respecting values, but I get only one parameter with the value beeing 37" w="100" h="70 我应该获得所有参数及其对应的值,但是我只能得到一个参数,其值是beeing 37“ w =” 100“ h =” 70
I know I am close, but this one is tricky 我知道我很近,但是这个很棘手
regards 问候
I don't think you need all the \\w
. 我认为您不需要所有
\\w
。 And I also would suggest splitting the task in two parts as suggested in a comment. 我也建议按照评论中的建议将任务分为两部分。
However, I also see an option in doing it in just one step : 但是,我也看到了一个选择,只需一步即可 :
\[\[\[img(?:\s(\w+)="([^"]+)")?(?:\s(\w+)="([^"]+)")?(?:\s(\w+)="([^"]+)")?\]\]\]
This is basically the wrapper [[[]]]
, a normal character part img
and then (?:\\s(\\w+)="([^"]+)")?
repeated as many times as you expect attributes to appear. (\\w+)
matches the name of the attribute and ([^"]+)
its value. 这基本上是包装器
[[[]]]
,是正常字符部分img
,然后是(?:\\s(\\w+)="([^"]+)")?
重复出现的次数与您期望的属性一样。 (\\w+)
匹配属性的名称和([^"]+)
的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.