Say I had this text string all over a database table field: &mc_gross_4=9.00&
The field title mc_gross is constant and the & before and after is constant, but the number 1, 2, 3 and so on holds an amount (in this case, 9.00). I need to parse the amount (9.00) for each &mc_gross_xx where xx is a number.
Thanks for your help.
Mike
How would I parse this text in LiveCode?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 361
- Joined: Wed Apr 27, 2011 2:12 pm
Re: How would I parse this text in LiveCode?
Hmmm. What would Klaus say?
Oh, yeah. "The itemDelimiter is your friend".
The trick, if you can rely on it, it to find a character or characters that are consistent in the string. In this case the "=" seems to be the one. If you set the itemDelimiter to that char, then the second item in each string will be your value of interest, along with the trailing "&". But this, too, seems to be consistent, so you can lose it explicitly.
Can you do this? Start with the dictionary for "itemDelimiter", then practice, and only then write back if you get stuck.
Craig Newman
Oh, yeah. "The itemDelimiter is your friend".
The trick, if you can rely on it, it to find a character or characters that are consistent in the string. In this case the "=" seems to be the one. If you set the itemDelimiter to that char, then the second item in each string will be your value of interest, along with the trailing "&". But this, too, seems to be consistent, so you can lose it explicitly.
Can you do this? Start with the dictionary for "itemDelimiter", then practice, and only then write back if you get stuck.
Craig Newman
Re: How would I parse this text in LiveCode?
Or try:
tFirstNum will have the 4 from the first token, and tSecondNum will have the floating point number after the equal sign. I tested this against your input string and got "4" and "9.00"
Walt
Code: Select all
matchText(tMyStringToSearch,"gross_([0-9]*)=([.0-9]+)",tFirstNum,tSecondNum)
Walt
Walt Brown
Omnis traductor traditor
Omnis traductor traditor
Re: How would I parse this text in LiveCode?
If you want to do it "the long way", i.e. not using regular expressions, there are many ways to do it.
If you want to get both the suffix and the value,
If you want to just get the value,
If you want to get both the suffix and the value,
Code: Select all
set the itemDelimiter to "="
put item 1 of yourThing into mc_gross_suffix
delete char 1 to 10 of mc_gross_suffix
put item 2 of yourThing into theValue
delete last char of theValue
If you want to just get the value,
Code: Select all
delete char 1 to 10 of yourThing
delete last char of yourThing