简体   繁体   English

正则表达式,带有数字和一些特殊字符

[英]Regex with digits and some special characters

I need help with creating a regex that would recognize tokens in text. 我需要有关创建可识别文本中标记的正则表达式的帮助。
Token requirements are as follows: 令牌要求如下:

  • should start and end with $ 应该以$开头和结尾
  • can contain any combination of letters, digits, _ 可以包含字母,数字和_任意组合
  • can contain only one consecutive . 只能包含一个连续的. , meaning: ,意思是:
    • $some.valid.sample$ is valid $some.valid.sample$有效
    • $some..invalid.sample$ is not $some..invalid.sample$不是
  • can contain square brackets, but only if they contain a number inside, meaning: 可以包含方括号,但前提是方括号内包含数字,表示:
    • $some.valid[0].sampl$ is valid $some.valid[0].sampl$有效
    • $some.invalid[].sample$ is not $some.invalid[].sample$不是
  • contains between 1 and 64 characters 包含1至64个字符

additional requirements (after discussion in comments): 附加要求(在评论中讨论之后):

  • square brackets with number have to be followed by . 带数字的方括号后必须跟. if they are not at the end (ie if they are not just before closing $ ) 如果它们不在结尾处(即,如果它们不只是在结束$之前)
  • length constraint applies to content between two $ 长度限制适用于两个$之间的内容

Can anyone help me out with this? 谁能帮我这个忙吗?
So far I have \\$([A-Za-z0-9._]*(\\[\\d+\\])*)+]$ 到目前为止,我有\\$([A-Za-z0-9._]*(\\[\\d+\\])*)+]$

根据注释中更新的规则,这是您需要的:

\$(?=[^\$]{1,64}\$)\w+(?:\[\d+\])?(?:\.\w+(?:\[\d+\])?)*\$

How about this pattern: 这个模式怎么样:

\$\w+?.?\w*?(\[\d+\]?)?.?\w*?\$

You can test it here 你可以在这里测试

it is a little too narrow though. 虽然有点太狭窄了。 I would take the suggestion by @Wiktor Stribizew 我会接受@Wiktor Stribizew的建议

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

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