Anuj More
Anuj More

Reputation: 22101

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.

Upvotes: 1

Views: 96

Answers (2)

Anuj More
Anuj More

Reputation: 22101

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

Upvotes: 1

vks
vks

Reputation: 67988

\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

Upvotes: 2

Related Questions