C#正则表达式以空格分割,但排除在[:和:]特殊字符之间的文本

huangapple go评论97阅读模式
英文:

C# Regex to split using space but exclude text between [: and :] special characters

问题

  1. 需要正则表达式来拆分文本,但排除**[::]**特殊字符之间的文本。
  1. one
  2. [two two]
  3. three
  4. [:four four:]
  5. five
  6. six
  7. [seven seven:]
  8. [:eight eight]
  1. 需要正则表达式来拆分文本,但排除**[::]特殊字符之间的文本以及[]**特殊字符之间的文本。
  1. one
  2. [two two]
  3. three
  4. [:four four:]
  5. five
  6. six
  7. [seven seven:]
  8. [:eight eight]
英文:

I need two C# Regular expressions to split using space but exclude text between [: and :] special characters.

  1. Need regex to split text but exclude text between [: and :] special characters.
  2. Need regex to split text but exclude text between [: and :] and also the text between [ and ] special characters.

Example: 1st Case:

  1. string input1 = "one [two two] three [:four four:] five six [seven seven:] [:eight eight]";

output:

  1. one
  2. [two
  3. two]
  4. three
  5. [:four four:]
  6. five
  7. six
  8. [seven
  9. seven:]
  10. [:eight
  11. eight]

Example: 2nd Case:

output:

  1. one
  2. [two two]
  3. three
  4. [:four four:]
  5. five
  6. six
  7. [seven
  8. seven:]
  9. [:eight
  10. eight]

I tried this but not working, producing below output

  1. string input1 = "one [two two] three [:four four:] five six [seven seven:] [:eight eight]";
  2. var parts1 = Regex.Matches(input1, @"[[::]].+?[\[::]]|[^ ]+").Cast<Match>()
  3. .Select(m => m.Value)
  4. .ToArray();
  1. one
  2. [two two] three [:four four:]
  3. five
  4. six
  5. [seven seven:]
  6. [:eight
  7. eight]

答案1

得分: 1

  1. var pattern = @"\[:[^][]*:]|\[(?!:)[^][]*(?<!:)]|\S+";
  2. var results = Regex.Matches(text, pattern).Cast<Match>().Select(x => x.Value);

详情

  • \[: - [: 字符串
  • [^][]* - 零个或多个非 [] 的字符
  • :]
  • | - 或
  • \[ - 一个 [ 字符
  • (?!:) - 紧接在右侧,不能是 : 字符
  • [^][]* - 零个或多个非 [] 的字符
  • (?<!:)] - 一个 ] 字符,前面不能有 :
  • | - 或
  • \S+ - 一个或多个非空白字符。
  1. <details>
  2. <summary>英文:</summary>
  3. You can use
  4. ```c#
  5. var pattern = @&quot;\[:[^][]*:]|\[(?!:)[^][]*(?&lt;!:)]|\S+&quot;;
  6. var results = Regex.Matches(text, pattern).Cast&lt;Match&gt;().Select(x =&gt; x.Value);

See the regex demo.

Details:

  • \[: - [: string
  • [^][]* - zero or more chars other than [ and ]
  • :]
  • | - or
  • \[ - a [ char
  • (?!:) - immediately on the right, there should be no :
  • [^][]* - zero or more chars other than [ and ]
  • (?&lt;!:)] - a ] char that has no : right before it
  • | - or
  • \S+ - one or more non-whitespace chars.

答案2

得分: 0

I think your input case might be wrong, it should be string input1 = "one [two two] three [:four four:] five six [:seven seven:] [:eight eight:]";

And you can use this regex to have the desired array:

var parts1 = Regex.Replace(input1, @"\[\:[a-z ]+\:\] *|\[[a-z ]+\] ", "").Trim().Split(' ');

英文:

I think your input case might be wrong, it should be string input1 = &quot;one [two two] three [:four four:] five six [:seven seven:] [:eight eight:]&quot;;

And you can use this regex to have the desired array:

var parts1 = Regex.Replace(input1, @&quot;\[\:[a-z ]+\:\] *|\[[a-z ]+\] *&quot;, &quot;&quot;).Trim().Split(&#39; &#39;);

huangapple
  • 本文由 发表于 2023年2月14日 21:37:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定