Convert 1 (540) 444-2045 to 15404442045? - Solved

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by DR White »

Is the an easy way to Convert 1 (540) 444-2045 to 15404442045?

If there isn't, no big deal.

I can use a several Offset functions to clean it up, but thought there might be an easier way.

Thanks,

David
Last edited by DR White on Mon Mar 20, 2017 4:12 pm, edited 1 time in total.
LiveCode_Panos
Livecode Staff Member
Livecode Staff Member
Posts: 870
Joined: Fri Feb 06, 2015 4:03 pm

Re: Convert 1 (540) 444-2045 to 15404442045?

Post by LiveCode_Panos »

Hi DR White,

What about this?

Code: Select all

on mouseUp
   answer myFun("1 (540) 444-2045")
end mouseUp

function myFun pIn
   local tOut
   repeat for each char tChar in pIn
      if isNumber(tChar) then
         put tChar after tOut
      end if
   end repeat
   return tOut
end myFun
Best,
Panos
--
DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Re: Convert 1 (540) 444-2045 to 15404442045?

Post by DR White »

Panos,

That works GREAT!

Thanks,

David
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by dunbarx »

Just for a different way to think about stuff:

Code: Select all

replace space with empty in yourString
replace "(" with empty in yourString
replace ")" with empty in yourString
replace "-" with empty in yourString
Craig Newman
AndyP
Posts: 634
Joined: Wed Aug 27, 2008 12:57 pm
Contact:

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by AndyP »

In one line :D

Code: Select all

 put replacetext("1 (540) 444-2045","[^0-9]","") 
Andy .... LC CLASSIC ROCKS!
DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by DR White »

Craig and Andy,

Thanks for the other solutions for converting mixed text to numbers.

Andy,

Where in the world to you find the option [^0-9] for replacetext?

When I use:

put "1 (540) 444-2045" into Test
put replacetext(Test,"[^0-9]","")

The string Test has not been converted>

Thanks,

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

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by Thierry »

DR White wrote: When I use:

put "1 (540) 444-2045" into Test
put replacetext(Test,"[^0-9]","")

The string Test has not been converted>
Hi David,

the converted text is the output of the function replacetext()

Therefore, try this:

Code: Select all

put "1 (540) 444-2045" into Test
put replacetext(Test,"[^0-9]","") into Test
put Test
HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by Klaus »

Hi David,
DR White wrote:When I use:

Code: Select all

    put "1 (540) 444-2045" into Test
   put replacetext(Test,"[^0-9]","")
The string Test has not been converted
"replacetext" is a function and does not modify its parameters!
But its OUTPUT is what you need! :D

Code: Select all

...
put "1 (540) 444-2045" into Test
put replacetext(Test,"[^0-9]","") into TEST2
answer TEST2
## = converted string!
...
Best

Klaus

P.S.
Ah, Thierry beat my by a couple of minutes :)
DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by DR White »

Thanks Klaus and Thierry!
DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by DR White »

Thierry,

Where did you find the info to use "[^0-9]"? in the replacementtext function?

Thanks,

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

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by Thierry »

DR White wrote:Thierry,

Where did you find the info to use "[^0-9]"? in the replacementtext function?
David,

This one is a copied/pasted of Andy's post :)

More seriously must be some information in the dictionary,
plus a RegexBuilder plugin
IDE Menu -> Development->Plugins->RegexBuilder
and I remember about a very simple stack with more details about regex
in sample stacks delivered with the IDE: still there?

The net is full of information about regex
but be careful there are a lot of different flavors of regex,
depending mainly of the regex engine and the language embedding it.

Kind regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
AndyP
Posts: 634
Joined: Wed Aug 27, 2008 12:57 pm
Contact:

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by AndyP »

Initially I did a Google search with "regex dictionary exclude non numbers" which came up with lots of hits using this "[^0-9]".

eg. http://stackoverflow.com/questions/3055 ... -or-period

Then I found some good explanation sources for Regex here

http://www.visca.com/regexdict/tutorial.html

http://www.pnotepad.org/docs/search/reg ... pressions/
Andy .... LC CLASSIC ROCKS!
SparkOut
Posts: 2984
Joined: Sun Sep 23, 2007 4:58 pm

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by SparkOut »

Thierry fell into the regex barrel when he was a baby, and had to eat his way through the whole lot to save himself from suffocation.
Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by Thierry »

SparkOut wrote:Thierry fell into the regex barrel when he was a baby,
and had to eat his way through the whole lot to save himself from suffocation.
the truth is, at this time, I had one foot in a regex barrel
and the other one in the Hypercard barrel!


Enjoy your day :)
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Post Reply