I have made the code below to find the value of a person called "Name Surname". The value is 0,5 as it is seen in the field content.
I want to get the result in the message box.
Seems like the code works because it doesn't return "not found". However, I only get an empty message box. What is the mistake in the code?
on mouseUp
answer funwithRegex( "Name Surname" )
end mouseUp
function funwithRegex theName
put quote into q
repeat for 44
put space after s
end repeat
if matchText( field 1, "(?m)<td class="&q&"kfnt"&q&" bgcolor="&q&"#ffffff"&q&" height="&q&"20"&q&"> "&theName&"</td>\n"&s&"<td class="&q&"kfnt"&q&" bgcolor="&q&"#ffffff"&q&" height="&q&"20"&q&" width="&q&"107"&q&">\n"&s&"<p align="&q&"center"&q&">([\d,]+)</p>\n"&s&"</td>", theValue) then
else
return "not found"
end if
end funwithRegex
You forgot the line "return theValue". Also, you can generate spaces much more quickly. I would put spaces around ampersands. That makes your script much(!) more readable.
function funwithRegex theName
put quote into q
set the itemDel to space
put space into item 44 of s
if matchText( field 1, "(?m)<td class="&q&"kfnt"&q&" bgcolor="&q&"#ffffff"&q&" height="&q&"20"&q&"> "&theName&"</td>\n"&s&"<td class="&q&"kfnt"&q&" bgcolor="&q&"#ffffff"&q&" height="&q&"20"&q&" width="&q&"107"&q&">\n"&s&"<p align="&q&"center"&q&">([\d,]+)</p>\n"&s&"</td>", theValue) then
return theValue
else
return "not found"
end if
end funwithRegex
Kind regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
This code doesn't work for the text below. It always returns "not found". I don't know why. However, there is something I have noticed. There are 6 spaces, but spaces are not normal.
on mouseUp
answer funwithRegex( "NAME SURNAME" )
end mouseUp
function funwithRegex theName
put quote into q
set the itemDel to space
put space into item 6 of s
if matchText( field 1, "(?m)<td bgcolor=" &q& "#FFFFFF" &q& " class=" &q& "kfnt" &q& " height=" &q& "20" &q& "> " & theName & "</td>\n" &s& "<td bgcolor=" &q& "#FFFFFF" &q& " class=" &q& "kfnt" &q& " width=" &q& "107" &q& " height=" &q& "20" &q& "><p align=" &q& "center" &q& ">([\d,]+)</td>", theValue) then
return theValue
else
return "not found"
end if
end funwithRegex
In the field text in my last reply, it seems there are many spaces, but indeed there are only 6. When I click edit. I can see it is 6. However, they are bigger than normal space character -maybe as big as TAB key.
I copied the space and check for the ascii code. It is a Tab indeed. I got ASCII 9. I have changed the code. Instead of space, I have used this lines, but no difference.
function seriouswithRegex theName
put "(?msi)<td\s.*?class=.kfnt..*?;" & theName & "</td>[^<]+" into r1
put "<td\s.*?class=.kfnt..*?<p align=.center.>([^<]+?)</" into r2
put r1 & r2 into regex
if matchText( field 1, regex, theValue) then
return theValue
else
return "not found"
end if
end seriouswithRegex
Here I write the same regex as 2 posts above, but splitting the regex by part
and trying to comment every step.
Umm, let me know if it's easier to get into it then...
One trick writting regex *in* Livecode (but be careful with it!):
LC compiler being unhappy with: class=\"kfnt\" bgcolor=\"
and to avoid those kind of hellish lines: class=" "e& "kfnt" "e& " bgcolor=" "e& "
we can use a '.' instead of \", then: class=.kfnt. bgcolor=.
Obviously, this can catch something like: class=XkfntX bgcolor=Z which is not the case in our context
function seriouswithRegex theName
local theValue
-- As we need 2 times next pattern, we can put it in a LC var
local KFNTclass
-- <td class="kfnt" bgcolor="#ffffff" height="20"> 
-- <td bgcolor="#FFFFFF" class="kfnt" width="107" height="20">
put "<td\s.*?class=.kfnt..*?" into KFNTclass
-- multiline, dot catches \n and not case sensitive
get "(?msi)"
-- <td bgcolor="#FFFFFF" class="kfnt" height="20"> Thierry Douez</td>
-- <td class="kfnt" bgcolor="#ffffff" height="20"> Aras fromTurkey</td>
-- theName is enclosed by ';' then '</td>'
-- and we don't care about other chars before ';'
get IT & KFNTclass & ";" & theName & "</td>"
-- consume spaces tabs and returns before next '<'
get IT & "[^<]+"
-- <td class="kfnt" bgcolor="#ffffff" height="20" width="107">
-- <p align="center">
-- <td bgcolor="#FFFFFF" class="kfnt" width="107" height="20"><p align="center">
get IT & KFNTclass & "<p align=.center.>"
-- capture number before </td> or </p>
get IT & "([^<]+?)</"
if matchText( field 1, IT, theValue) then return theValue
return "not found"
end seriouswithRegex