Extracting numbers from a string

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
kevin007
Posts: 40
Joined: Mon Sep 21, 2015 7:46 am

Extracting numbers from a string

Post by kevin007 »

Hi ALL

I have an variable that contain following string .
[xshift={\hzwidth+10} ,yshift={-\vtheight+15}, rotate=30]
I want only numbers from above string and save these three numbers to three different variable :(
--
Thanks
Kevin
Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Extracting numbers from a string

Post by Thierry »

kevin007 wrote:
[xshift={\hzwidth+10} ,yshift={-\vtheight+15}, rotate=30]
I want only numbers from above string and save these three numbers to three different variable :(
Hi Kevin,

If you don't have any allergia with regex, here is one solution:

Code: Select all

if matchText( S, "\+(\d+)}.+\+(\d+)}.+=(\d+)]", n1, n2, n3) then
      put "Find: " & n1 && n2 && n3
   else
      put "Sorry, no match!"
   end if
Just typed it out of my head, so beware of possible typos.

Best,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
kevin007
Posts: 40
Joined: Mon Sep 21, 2015 7:46 am

Re: Extracting numbers from a string

Post by kevin007 »

@Thierry
superb coding :)
--
Thanks
Kevin
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Extracting numbers from a string

Post by dunbarx »

Thierry can do more with one line of regex than I can in a handful of lines of "ordinary" code.

But are you OK with that, or do you want to talk about those handfuls?

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

Re: Extracting numbers from a string

Post by Thierry »

dunbarx wrote:Thierry can do more with one line of regex than I can in a handful of lines of "ordinary" code.
Hi Craig,

Mmm, I have nothing to do with this fact; that's the power of regex: concise and powerful for
pattern matching.
But are you OK with that, or do you want to talk about those handfuls?
Well, I think it will be nice to have some 'Livecodish' solutions, and let the readers learn,
compare and experiment out of them.
My 2 cents,

@Kevin: Re-reading my post, I've noticed there are some room for speeding up the regex.
If you feel it's too slow, just let me know.

But now, I have to unplug all my computers as a big storm is coming.

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: Extracting numbers from a string

Post by Klaus »

Bonsoir Thierry,
Thierry wrote:
dunbarx wrote:Thierry can do more with one line of regex than I can in a handful of lines of "ordinary" code.
Hi Craig,

Mmm, I have nothing to do with this fact; that's the power of regex: concise and powerful for
pattern matching.
JAMAIS, mon ami!
I still consider this WITCHCRAFT! :D


Have a nice weekend!

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

Re: Extracting numbers from a string

Post by Thierry »

Klaus wrote:Bonsoir Thierry,

JAMAIS, mon ami!
I still consider this WITCHCRAFT! :D
So be careful when talking to me that I'm not going to cast a spell on you :)

Schönes Wochenende

Regards,

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: Extracting numbers from a string

Post by Klaus »

Thierry wrote:
Klaus wrote:Bonsoir Thierry,

JAMAIS, mon ami!
I still consider this WITCHCRAFT! :D
So be careful when talking to me that I'm not going to cast a spell on you :)

Schönes Wochenende

Regards,

Thierry
:D :D :D
Post Reply