Regex for extracting digits in a random string

Macro_Mitch

New member
Hello. Can someone please help how i can extract the only 4 digits available in a string eg:
1 "gajkd4ksks8nsh2hjs7ks"
2 "2jdjjd4jjsj7hsh3"
3 "Gsgs5926jsjd"
4 "HshhhdhklakkGhsh5277"
Note that the strings can be of different lengths and the digits might appear randomly in the string they don't follow a specific oder of appearance. I've tried the below regex (\d{4}) but it only works when the digits are placed together like in example 3. Thank you in advance
 

Dm114

Well-known member
Any help in returning the sum of the extracted digits eg 6278 to be 6+2+7+8 which is 23
My previous suggestion only works for single digit numbers, i.e. from 0 to 9.

To be more versatile and extract whole numbers (not only digits) you could replace the Extract text AND the Replace text options by a unique Replace option with the following parameters:
- Source text: your source text with a mix of letters and digits
- Text to replace (regex): \D*(\d+)\D*
- New text: +$1

Unless you consider each digit is as whole number. Then a unique Replace text option with \D*(\d)\D* will make the job.

Then, as shown previously, set a numerical variable with the content of this new String variable...

Good luck!
 
Top