简体   繁体   中英

Regex to find all matches of from a string

I have the string:

TEXT OF SWITZERLAND CASABLANCA, 2041 Cash 1234e

I want to extract all alphanumeric values ( digits compulsory ) with length of at least 4, with or without the inclusion of the special characters /\\_:.\\-|

Below is the Regex that I have tried.

(?=.{4,}$)(?=.*[0-9])([a-zA-Z0-9/\\\\_:.\\-|]+)$

But this only captures the required pattern if it is last in the string. I want to capture all the values. In this case 2041 and 1234e .

I have tried solutions from the answers and a few more but none worked.

\b(?=[a-zA-Z]*\d)[a-zA-Z0-9/\\_:.\-|]{4,}\b

Try this. $ is causing only the last match to be found. \\b will mark the word boundary and you get all the matches.See demo

https://www.regex101.com/r/fJ6cR4/15#python

Adding to VKS's answer, the regex could not match the string ABSTRD.910.824

I tried this and it worked perfectly.

Regex: \\b(?=[a-zA-Z/\\\\_:\\-.|]*\\d)[a-zA-Z0-9/\\\\_:\\-.|]{4,}\\b

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