字符串所需的正则表达式 [英] Regular expression required for string

查看:96
本文介绍了字符串所需的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的字符串:



< 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
20170118062717

And 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆