[英]Regex to match all the strings between two identical strings
Eg I have this string -- This -- is -- one -- another -- comment --
I want the matched elements to be "This", "is", "one", "another", and "comment"例如,我有这个字符串-- This -- is -- one -- another -- comment --
我希望匹配的元素是“This”、“is”、“one”、“another”和“comment”
I was trying this regex --\\s+([^--]+)\\s+--
which gives me the matched elements as "This", "one" and "comment"我正在尝试这个正则表达式--\\s+([^--]+)\\s+--
它给了我匹配的元素作为“这个”、“一个”和“评论”
I have searched for other problems, they all provide solution like this ie #A#
and I will get A
but for #A#B#
also I get A
, but in this case I want both the elements A
and B
as both of them are between two #
chars.我已经搜索了其他问题,它们都提供了这样的解决方案,即#A#
并且我会得到A
但是对于#A#B#
我也会得到A
,但在这种情况下我想要元素A
和B
因为它们都介于两者之间两个#
字符。
I am testing it for javascript regex, but I think solution should be irrespective of platform/language.我正在为 javascript regex 测试它,但我认为解决方案应该与平台/语言无关。
In general, you need to use a pattern like通常,您需要使用类似的模式
STRING([\s\S]*?)(?=STRING|$)
It will match STRING
, then capture into Group 1 any zero or more chars, as few as possible, up to the first occurrence of STRING
*stopping right before this word** because the (?=...)
is a positive lookahead that, being a zero-width assertion, does not consume matched text or end of string.它将匹配STRING
,然后将任何零个或多个字符捕获到 Group 1 中,尽可能少,直到第一次出现STRING
*在这个词之前停止** 因为(?=...)
是一个积极的前瞻,作为零宽度断言,不消耗匹配的文本或字符串的结尾。
A generic variation of the pattern is该模式的一般变体是
STRING((?:(?!STRING)[\s\S])*)
It uses a tempered greedy token , (?:(?!STRING)[\\s\\S])*
, that matches any char, 0 or more occurrences, that does not start a STRING
char sequence.它使用一个缓和的贪婪令牌(?:(?!STRING)[\\s\\S])*
,它匹配任何字符,0 次或多次出现,不开始STRING
字符序列。
To get all the substrings in the current solution, use a lookahead like要获取当前解决方案中的所有子字符串,请使用类似的前瞻
/--\s+([\s\S]*?)(?=\s+--)/g
^^^^^^^^^
See the regex demo .请参阅正则表达式演示。
Note that [^--]+
matches 1 or more symbols other than a -
, it does not match any text that is not equal to --
.请注意, [^--]+
匹配 1 个或多个除 a -
之外的符号,它不匹配任何不等于--
文本。 [...]
is a character class that matches a single character. [...]
是匹配单个字符的字符类。 To match any text of any length from one char up to the first occurrence of a pattern, you can rely on a [\\s\\S]*?
要匹配从一个字符到模式第一次出现的任何长度的任何文本,您可以依赖[\\s\\S]*?
construct: any 0+ chars, as few as possible (due to the lazy *?
quantifier).构造:任何 0+ 个字符,尽可能少(由于惰性*?
量词)。
JS demo: JS演示:
var s = '-- This -- is -- one -- another -- comment --'; var rx = /--\\s+([\\s\\S]*?)(?=\\s+--)/g; var m, res=[]; while (m = rx.exec(s)) { res.push(m[1]); } console.log(res);
To read all I would use positive look ahead:要阅读所有内容,我会使用积极的展望:
const data = '-- This -- is -- one -- another -- comment --' const readAll = data => { const regex =/--\\s*(.*?)\\s*(?=--)/g const found = [] let temp while (temp = regex.exec(data)) { found.push(temp[1]) } return found } console.log(readAll(data))
And to remove comments just do this:要删除评论,只需执行以下操作:
const data = `-- This -- is -- one -- another -- comment -- this is not a comment`.replace(/--.*--/g, '') console.log(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.