简体   繁体   English

匹配包含以4位正则表达式结尾的特定子字符串的字符串

[英]match string containing specific substring ending on 4 digits regex

I am trying to match the following type of string: 我正在尝试匹配以下类型的字符串:

Match 比赛
- 4738 -4738
- 3333 -3333

No match 没有匹配
- 0447 7474 -0447 7474
9495 - 8485 9495-8485
/2848 / 2848
/ 9949 / 9949
- 584888 -584888

so i made this regex: /^(- )?(?=\\d{4})$/ but this doesnt match my target string: - 8549 所以我做了这个正则表达式: /^(- )?(?=\\d{4})$/但这与我的目标字符串不匹配: - 8549
this is data i need to analyse: 这是我需要分析的数据:
在此处输入图片说明

I am quite new to regex so all tips and suggestions are welcome on how to fix this. 我是regex的新手,因此欢迎提供所有技巧和建议以解决此问题。

Thanks in advance 提前致谢

This should work: 这应该工作:

/^(- )?(\d{4})$/

if you don't need to capture any groups: 如果您不需要捕获任何组:

/^(?:- )?\d{4}$/

如果您尝试/^-\\s\\d{4}$/ ,它应该可以解决问题;)

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

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