简体   繁体   English

我该如何写一个正则表达式来匹配首尾之间的所有内容?

[英]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.

相关问题 如何编写一个正则表达式,该表达式匹配除左括号和右括号以及它们之间的所有内容以外的所有内容 - How do I write a regular expression that matches everything except a left and right parenthesis and anything between them 如何编写以任意顺序匹配两个或多个字符的正则表达式? - How can I write a regular expression that matches two or more characters in any order? 如何创建一个匹配给定字符后一行上所有内容的正则表达式? - How do I make a regular expression that matches everything on a line after a given character? 如何在JavaScript中从正则表达式匹配中调用函数 - How can I call a function from regular expression matches in javascript 如何将未编码的Json写入我的View并发出第一个和最后一个报价 - How do I write unencoded Json to my View and emit the first and last quote 我如何编写 Javascript 正则表达式模式来处理这些情况 - How can I write the Javascript Regular Expression pattern to handle these conditions 如何通过正则表达式在对象值中指定单引号 - How do I Specify single quote in object value by regular expression 正则表达式匹配除前两个字符外的所有内容 - Regular Expression to match everything but the first two characters 如何使用正则表达式使用Javascript替换字符串中特定单词之外的所有内容 - How can I use a Regular Expression to replace everything except specific words in a string with Javascript 在正则表达式中查找第一个匹配字符 - find first matches character in regular expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM