How to include character in regular expression

南笙酒味 提交于 2021-01-28 07:11:45

问题


I have this text:

x A1 ,A2-A4 xx, xxx A5  xxxx  

I have this regular expression:

[A-Z]{1,3}-?\d{1,3}  

It gets 4 matches:

A1,A2,A4,A5  

But I need to modify this regular expression to get these 3 matches:

A1,A2-A4,A5    

Can anyone help me?


回答1:


Just double the expression:

[A-Z]{1,3}-?\d{1,3}(?:-[A-Z]{1,3}-?\d{1,3})?

Demo



来源:https://stackoverflow.com/questions/29124643/how-to-include-character-in-regular-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!