简体   繁体   中英

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..invalid.sample$ is not
  • can contain square brackets, but only if they contain a number inside, meaning:
    • $some.valid[0].sampl$ is valid
    • $some.invalid[].sample$ is not
  • contains between 1 and 64 characters

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+\\])*)+]$

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

\$(?=[^\$]{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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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