Regex to find phone numbers in text

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Regex to find phone numbers in text

Post by rodneyt » Mon Jul 26, 2021 2:39 am

Using Regex from this thread: https://stackoverflow.com/questions/166 ... one-number

I am trying to use matchtext to identify phone numbers in text.

Can someone tell me why the following doesn't work:

Passing in phone number text eg. something like

Code: Select all

some text and phone number(s) such as
123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+91 (123) 456-7890

Code: Select all

private command tpDetectPhoneNumber pt,@pDataA
	local tPhone
	get matchText(pt,"(^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$)",tPhone)
answer it && tPhone
More generally what do people recommend for testing and developing regex statements to try to find out why they are not working?

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: Regex to find phone numbers in text

Post by rodneyt » Mon Jul 26, 2021 5:26 am

Looking through the forum archives I found a reference to a regex testing/sharing site which other readers may find useful. Here's my regex on this site:
https://regex101.com/r/wFbXfW/1

It runs fine on this site, returning a result on test data. But does not work in Livecode.

Can anyone see what I'm doing wrong?

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: Regex to find phone numbers in text

Post by rodneyt » Mon Jul 26, 2021 5:59 am

Resolved: it didn't like the "$" sign at the end. This interactive debugging tool makes it very easy to see how your regex works - recommend this to anyone else trying to get going with matchtext.

stam
Posts: 2636
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Regex to find phone numbers in text

Post by stam » Mon Jul 26, 2021 8:03 am

I personally use this site: https://regexr.com
But it's much of a muchness...

--EDIT--
If you prefer to use an app rather than website, and you don't mind spending ~$10 for good, old fashioned shareware, i'd recommend this RegExRx: http://www.mactechnologies.com/index.ph ... ds#regexrx
Built with XOJO, available for mac/win.

For those of us who do not live & breathe regex, it makes it much easier to search for patterns etc as it has drop downs that let you select what you're looking for, but it also lets you copy/paste into various languages (although sadly does not cater specifically for LiveCode, but i've used it a lot for LC... plus i always support devs where they produce a good app that i use).
Or view it on the Mac Appstore with images: https://apps.apple.com/gb/app/regexrx/ ... 0702?mt=12

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: Regex to find phone numbers in text

Post by rodneyt » Mon Jul 26, 2021 11:15 pm

Thanks Stam, I will take a look at this tool. Regex like many command line utilities is a whole world unto itself, and the challenge is always how much to invest in learning it vs just "getting the job done". Tools that help you to learn as you go are good.

It would be good if the Livecode documentation had a built in feature to allow users to submit suggestions or link a topic in the forum directly to a documentation item.

~ Rodney

stam
Posts: 2636
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Regex to find phone numbers in text

Post by stam » Tue Jul 27, 2021 11:37 am

rodneyt wrote:
Mon Jul 26, 2021 11:15 pm
Tools that help you to learn as you go are good.
The problem is that i don't think many people (myself included) do enough regex to actually properly get over the learning curve. I know I use this occasionally and it's a great tool, it's just that there is not that much need for it and the little i learn when i dabble is mostly forgotten by the time i next have to use regex.

Thats' why i like RegExRx - if you vaguely know what you want, you can pick the right generic formula. Gets the job done and keeps me from tearing out whatever is left of my hair ;)

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Regex to find phone numbers in text

Post by Thierry » Wed Jul 28, 2021 3:05 pm

Hi Thierry,

As a regex forum expert I am hoping you might have a minute
to take a look at my post at viewtopic.php?f=9&t=36139

Interested in your thoughts on learning / getting better with Regex and Livecode.

~ Rodney
Hi Rodney,

Finally found some time to chime in with my little regex experience...


Ok, let start with the tool Stam suggests - RegExRX.

Here is a screenshot with your original regex
including the $ at the end of the regex:

screenshot 2021-07-28 à 12.37.54.jpg

First, we can see that this regex works here, but as you experimented not in LiveCode.
This is true with any other tools (apps or online-tools).
There are some context differences which can be very annoying if one don't know/understand them.

To name few:
- PCRE_lib versions
- language implementing PCRE (php, Xojo, python...)
- regex default options


That said, back to your regex which is constructed like: ( ^ .... $ )
In livecode by default, the carret points to the beginning of the input string, but not the beginning of a line
Same behavior with the $; end of string not end of line.
This is why it didn't work except if you change the mode: multi-lines -> (?m)

Now, rewriting your regex this way: (?m) ( ^ .... $ ), it should work.
Or you can simply ignore the carret and the $ and it should be fine too with the datas you provided earlier.

Investigating a bit further the regex, what about false positives?
Looking in the picture above the last lines which are not valid phone numbers but they match.
So, depending of what you need to achieve, this regex is good enough or bad.

Et voila mon ami, HTH.

Kind regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply

Return to “Talking LiveCode”