简体   繁体   中英

Regex to find words on strings that start with specific character, contains 2-5 characters and is not composed by only digits

I can have one of the following sample strings:

  1. Apple $Banana Kiwi
  2. Apple $TTL, Kiwi
  3. Apple $9999. Kiwi
  4. Apple $9as. Kiwi
  5. Apple $z.9s. Kiwi

I need to find the words contained by the string that start with '$' where lenght except '$' is between 2 and 5 and do not contains only digits and it can contains dots , without punctuation at end.

Inside the list above, number 2,4 and 5 match my criteria.

I tried to write a regex to do that without useful result.

(?<![a-z0-9])[$]{1}[A-Z0-9\.]{2,5}(?![a-z0-9])

I use Java to match regex

Is there a regex to do that?

Use a lookaround based regex like below.

(?<![a-z0-9])[$](?:(?!\d+\b)[A-Z0-9a-z.]){2,4}(?![a-z0-9])

DEMO

OR

(?<![a-z0-9])[$](?:(?!\d+\b)[A-Z0-9a-z]){2,4}(?![a-z0-9])

DEMO

(?<![a-zA-Z0-9])\$(?=\S*[a-zA-Z])[a-zA-Z0-9.]{2,4}(?=\s|$)

Try this.See demo.

https://regex101.com/r/mS3tQ7/15

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