Grep to find any number of 10 digits in a string of text

Hello all,
I am terrible in figuirng out how to use grep or regular expressions :frowning:

Assume I have a string like this:

I am currently using this code:


set userResponse to "adkfjkadjf a kadk eqyef ahda 31414141387193781 adkja 313413 hdf ahd 1234567890 abadf"
set numsFound to (do shell script "echo '" & userResponse & "' | grep -o '[0-9]\\{10\\}'")

This script will return:

β€œ3141414138
1234567890”

Unfortunately, I only want the grep to return the second set of 10 digits (1234567890) because it is an exact match in terms of being 10 digits. The first number β€œ3141414138”, which I don’t want is part of a longer number β€œ31414141387193781”, so I do not this to be considered as a match?

I hope this makes sense? I can definitely use the help, and my Internet searching was a failure.

Thanks,
-Jeff

Try this:


use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

set userResponse to "adkfjkadjf a kadk eqyef ahda 31414141387193781 adkja 313413 hdf ahd 1234567890 abadf"
set userResponse to current application's NSString's stringWithString:userResponse
set theRange to userResponse's rangeOfString:"(?<!\\d)\\d{10}(?!\\d)" options:(current application's NSRegularExpressionSearch)
return (userResponse's substringWithRange:theRange) as text

A basic AppleScript suggestion:

set userResponse to "adkfjkadjf 0987654321 a kadk eqyef ahda 31414141387193781 adkja 313413 hdf ahd 1234567890 abadf"

set theNumbers to {}
set theNumbersList to {}

repeat with aCharacter in userResponse
	set aCharacter to contents of aCharacter
	try
		aCharacter as integer
		set end of theNumbers to aCharacter
	on error
		if (count theNumbers) = 10 then set end of theNumbersList to (theNumbers as text)
		set theNumbers to {}
	end try
end repeat

theNumbersList --> {"0987654321", "1234567890"}

As written, this script took 4 milliseconds to run, which is faster than running the do-shell-script command by itself.

Thank you both. Both solutions work very well, and I may use it. But there are rare situations where the string might connatain two valid 10 digit numbers? I would like the resturned list to contain both items, which would be two separate 10 digit number strings?

Jeffkr. I have modified my above script to do what you want. BTW, my script works well with a short string but would become slow if the string is long. In that case, Shane’s script would certainly be preferred.

This:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

set userResponse to "adkfjkadjf a kadk eqyef ahda 31414141387193781 adkja 313413 hdf ahd 1234567890 hdf ahd 2234567890 abadf"
set userResponse to current application's NSString's stringWithString:userResponse
set regex to current application's NSRegularExpression's regularExpressionWithPattern:"(?<!\\d)\\d{10}(?!\\d)" options:0 |error|:(missing value)
set theFinds to regex's matchesInString:userResponse options:0 range:{0, userResponse's |length|()}
set theResults to {}
repeat with aFind in theFinds
	set end of theResults to (userResponse's substringWithRange:(aFind's range())) as text
end repeat
return theResults

Or simply use my RegexAndStuffLib:

use AppleScript version "2.5" -- macOS 10.11 or later
use script "RegexAndStuffLib" version "1.0.6"
use scripting additions

set userResponse to "adkfjkadjf a kadk eqyef ahda 31414141387193781 adkja 313413 hdf ahd 1234567890 hdf ahd 2234567890 abadf"
set theResult to regex search userResponse search pattern "(?<!\\d)\\d{10}(?!\\d)"

https://latenightsw.com/support/freeware/

I really am very appreciative of you both - This is exactly what I was looking for!

Thank you very much.

-Jeff