正则表达式匹配的字符,而不是当它用方括号方括号、它用、而不是、字符

由网友(甘泉制酒)分享简介:输入字符串:[Wsg-Fs]-A-A-A-Cgbs-Sg7-[Wwg+s-Fs]-A-A-Afk-Cgbs-Sg7所需的输出是一个字符串数组:Desired output is a string array:[Wsg-Fs] A A A Cgbs Sg7 [Wwg+s-Fs] A A Afk Cgbs Sg7如果...

输入字符串:

[Wsg-Fs]-A-A-A-Cgbs-Sg7-[Wwg+s-Fs]-A-A-Afk-Cgbs-Sg7

所需的输出是一个字符串数组:

Desired output is a string array:

[Wsg-Fs] A A A Cgbs Sg7 [Wwg+s-Fs] A A Afk Cgbs Sg7

如果我拆分输入字符串 - 作为分隔符,方括号内的字符串也得到分流

If I split the input string with - as delimiter, the string within square brackets also gets split.

我如何分割字符串,这样 - 中括号应该被忽略

How do I split the string such that - within square bracket should be ignored?

我能找到一些类似的帖子试图忽略分隔符用引号引起来,但我一直没能应用这些解决我的问题。

I could find some similar posts trying to ignore delimiter enclosed in quotes, however I have not been able to apply those solution to my problem.

任何建议将是非常有益的。谢谢!

Any suggestion would be really helpful. Thanks!

推荐答案

假设没有嵌套的方括号中,你可以使用下面只匹配 - 字符是方括号外的:

Assuming there are no nested square brackets, you can use the following to only match - characters that are outside of square brackets:

-(?![^[]*])

例: http://regex101.com/r/sX5hZ2

这采用的是消极的超前,与逻辑,如果有在任何打开方括号右方括号,那么 - ,我们试图匹配的内括号内。

This uses a negative lookahead, with the logic that if there is a closing square bracket before any opening square brackets, then the - that we tried to match is inside of brackets.

阅读全文

相关推荐

最新文章