简体   繁体   English

用'['和']'分割字符串

[英]Split a string with '[' and ']'

I would like to know the RegEx Split pattern (using C#) for a string which packed with [ and ] . 我想知道使用[]打包的字符串的RegEx Split模式(使用C#)。

For example, for the string: 例如,对于字符串:

This is my [word1] And this is my [word2]

I should get word1 and word2 . 我应该得到word1word2

The following should do the trick: 以下应该可以解决问题:

\[(?<word>[^\]]*)\]

I would suggest you use some regex tool to assist you. 我建议您使用一些正则表达式工具来帮助您。 I'm sure there are a couple but I use Expresso and it really helps with these pesky things :) 我敢肯定有几个,但是我使用Expresso ,它确实可以帮助解决这些令人讨厌的事情:)

Use regex /\\[(.*?)\\]/g . 使用正则表达式/\\[(.*?)\\]/g Given regex is in perl, similar is in other langauges too. 鉴于正则表达式位于perl中,其他语言也是如此。 Worked out here: https://regex101.com/r/zL0yW1/1 在这里工作: https : //regex101.com/r/zL0yW1/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM