简体   繁体   English

用正则表达式分割复杂的字符串

[英]Splitting complicated string with regex

I have very basic knowledge of regular expressions and this one is just too hard for me. 我对正则表达式非常了解,这一点对我来说太难了。 I need to split string into three parts. 我需要将字符串分成三部分。

My input looks like this XXX YYY:ZZZ: (always ends with :ZZZ: ) 我的输入看起来像这样XXX YYY:ZZZ:总是以:ZZZ:结尾)

  • XXX can be anything including $&+,:;=?@#|'<>.-^*()%! XXX可以是$&+,:;=?@#|'<>.-^*()%! and spaces 和空格
  • YYY are alphabetical chars which can end with . YYY是字母字符,可以以结尾. or are not there at all 还是根本不在那里
  • ZZZ is number which can have leading zeroes and is in between two : at the end of string ZZZ是可以以零开头的数字,介于两个之间:在字符串的末尾

Trickiest string I can come up with is Lo-rem ip sum.,::0001678154: . 我能想到的最棘手的字符串是Lo-rem ip sum.,::0001678154: ZZZ would be 0001678154 we can get rid off :ZZZ: so remaining part is Lo-rem ip sum.,: but last char is not either alphanumeric or . ZZZ0001678154我们可以摆脱:ZZZ: 0001678154所以剩余部分是Lo-rem ip sum.,:但最后一个字符不是字母数字或. so that means YYY is not included in this string XXX is whatever remained ( Lo-rem ip sum.,: ) 因此,表示此字符串中不包括YYY XXX就是剩下的值( Lo-rem ip sum.,:

Now I have horrible half functioning code with hardcoded lengths, ifs, substrings and splits I believe you could come up with better solution. 现在,我有了具有硬编码长度,if,子字符串和拆分的可怕的半功能代码,我相信您可以提供更好的解决方案。 Thanks 谢谢

The below regex would capture and store the three set of characters into three separate groups, 下面的正则表达式将捕获三组字符并将其存储到三个独立的组中,

^(?<xxx>.*?)(?<yyy>(?:[A-Za-z0-9])+\.?)?:(?<zzz>\d+):$

DEMO DEMO

? after the second group will make the group as optional. 在第二组之后将使该组成为可选。

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

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