字符串所需的正则表达式 [英] Regular expression required for string
问题描述
我有类似的字符串:
< pre> 1H | \ ^& ||| PACMAN ||| || PSM || P || 20170118062717 |
我想提取数据 H , PACMAN , PSM , P 和 20170118062717
什么可以是这个字符串的正则表达式吗?
我尝试过:
这就是我的尝试:
(?:^(R)\ |(\d +)\\ \\ | \ ^ *([^ |] *)\ |([^ |] *)\ |:* $)|((([^ | |] *)\?):??^ (P)\ |(\d +)\ | \ |(\d +)。* $)
)|(?:^(p)。\ |(\d +)\ | \ |(\d +)*
)
我从以下开始:
(?< = |)\w +
哪个会检索:
1H
PACMAN
PSM
P
20170118062717并手动抛弃前导零,或使用
([a-zA-Z] + | \d {2,})(?= |)
这很好。我可以分组获取这些值吗?
要将其用作组,您需要指定确切的布局 - 这并不难,但是取决于您的数据。如果样本具有代表性,并且您只需要填充列的组,但列由|分隔字符,然后尝试:
^ \d(\w +)(?: |。*?){3}(\ w +)(?:| 。*){5}(\w +)(?: |??*)(\w +)(?: |。?*)(\w +)|
I have String like:
<pre>1H|\^&|||PACMAN|||||PSM||P||20170118062717|
And I want to extract data H, PACMAN, PSM, P and 20170118062717
What can be the Regular Expression for this String?
What I have tried:
This is how I am trying:
(?:^(R)\|(\d+)\|\^*([^|]*)\|([^|]*)\|(?:([^|]*)\|)?.*$)|(?:^(P)\|(\d+)\|\|(\d+).*$)
)|(?:^(P)\|(\d+)\|\|(\d+).*
)
I'd start with:
(?<=|)\w+
Which would retrieve:
1H PACMAN PSM P 20170118062717And manually discard the leading zero, or use
([a-zA-Z]+|\d{2,})(?=|)
That's nice. Can I get these values in groups?
To use it as groups, you would need to specify the exact layout - which isn't difficult, but does depend on your data. If the sample is representative and you want groups for the "filled columns" only, but columns are delimited by "|" characters, then try:
^\d(\w+)(?:|.*?){3}(\w+)(?:|.*?){5}(\w+)(?:|.*?)(\w+)(?:|.*?)(\w+)|
这篇关于字符串所需的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!