Find a word in a field and record its location

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Find a word in a field and record its location

Post by ARAS » Sat Nov 02, 2013 6:06 pm

Hello,

How to find a word and find its location(what number is the word in the field) and use that information?

For example, according to the codes below, I need to find John and count 9 more characters and start recording from the 10th until you reach "<" and store it into a variable.

Code: Select all

<table border="1">
<tr>
<th>Name</th>
<th>Value</th>
</tr>
<tr>
<td>John</td>
<td>1.5</td>
</tr>
<tr>
<td>Mark</td>
<td>2</td>
</tr>
</table> 
So the variable will be "1,5"

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Find a word in a field and record its location

Post by Mark » Sat Nov 02, 2013 6:38 pm

Hi,

Do you really just want to find "John"? In that case you can simply do

put offset("John",myHtmlData) into myStart
put myStart + len("John") - 1 into myEnd

If you want to find something and then want to know the complete data inside the tag, you want to do this:

Code: Select all

put offset("John",myHtmlData) into myPosition
put 0 into myWalk
repeat until myPosition - myWalk is 1
  add 1 to myWalk
  if char (myPosition - myWalk) of myHtmlData is ">" then
    put myPosition - myWalk into myStart
    exit repeat
  end if
end repeat
put 0 into myWalk
repeat until myPosition + myWalk is len(myHtmlData)
  add 1 to myWalk
  if char (myPosition + myWalk) of myHtmlData is "<" then
    put myPosition + myWalk into myEnd
    exit repeat
  end if
end repeat
This might need some tweaking. For example, this script doesn't let you search for data containing < or >.

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

ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Re: Find a word in a field and record its location

Post by ARAS » Sat Nov 02, 2013 8:41 pm

Hi Mark,

Thanks for the help.

When I run your code under a button, LiveCode Community 6.5 (dp2) gets frozen.
However, when I change "myHTMLData" with "Field 1"(The text field that I am using), it doesn't get frozen.

Am I missing something? and Am I on the right path?

Also, which variable stores John's value?

ARAS

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Find a word in a field and record its location

Post by Mark » Sat Nov 02, 2013 8:56 pm

Hi,

Yes, you are on the right path. As I said, the code needs some tweaking. I don't have much time now.

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

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

Re: Find a word in a field and record its location

Post by Thierry » Sat Nov 02, 2013 9:00 pm

ARAS wrote:

Code: Select all

<table border="1">
<tr>...
<td>John</td>
<td>1.5</td>
....
</tr>
</table> 
So the variable will be "1,5"
Hi ARAS,

Saturday night regex fiever :)

So, that's the way I'ld do it:

Code: Select all

on mouseUp
   answer funwithRegex( "Mark" )
end mouseUp

function funwithRegex theName
   if matchText( field 1, "(?m)<td>" & theName & "</td>\n<td>(\d+)</td>", theValue) then
      return theValue
   else
      return "not found"
   end if
end funwithRegex
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Re: Find a word in a field and record its location

Post by ARAS » Sat Nov 02, 2013 9:39 pm

Thierry wrote:

Code: Select all

on mouseUp
   answer funwithRegex( "Mark" )
end mouseUp

function funwithRegex theName
   if matchText( field 1, "(?m)<td>" & theName & "</td>\n<td>(\d+)</td>", theValue) then
      return theValue
   else
      return "not found"
   end if
end funwithRegex
Thierry
Thanks Thierry!!!

It works when I search Mark. However, when I search John it says "not found"

ARAS

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

Re: Find a word in a field and record its location

Post by Thierry » Sat Nov 02, 2013 10:04 pm

ARAS wrote:
Thierry wrote:

Code: Select all

on mouseUp
   answer funwithRegex( "Mark" )
end mouseUp

function funwithRegex theName
   if matchText( field 1, "(?m)<td>" & theName & "</td>\n<td>(\d+)</td>", theValue) then
      return theValue
   else
      return "not found"
   end if
end funwithRegex
Thanks Thierry!!!
It works when I search Mark. However, when I search John it says "not found"

Hi ARAS,

quite late here, so I let you that as an exercice :)

tip:
in the regex I gave you, what is between () will be captured and put later into the LC variable theValue.

We have a \d+ pattern, which means one or more digit; a digit is 0, 1, 2,... 9

So, with John you have for the result 1.5; the '.' is not recognized by \d+
You need to change this pattern by a character class: [\d.]+ and then it should work...

With regex, you have to know your datas and find that way a pattern which fits with,
Then you have to know too what you do *not* want to match; this one is often a bit more difficult.

Good luck :)

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

ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Re: Find a word in a field and record its location

Post by ARAS » Sat Nov 02, 2013 11:25 pm

Thanks again Thierry.

It works like a charm.
you have to know too what you do *not* want to match; this one is often a bit more difficult.
Yes I can imagine from now. I will to be working on a way more complicated html table, and that's been my concern.

I didn't know those were called regex(regular-expressions). Thanks for useful tips and information.

Have a good night
ARAS

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

Re: Find a word in a field and record its location

Post by Thierry » Sun Nov 03, 2013 9:06 am

ARAS wrote: Thanks again Thierry.It works like a charm.
I will to be working on a way more complicated html table, and that's been my concern.
I didn't know those were called regex(regular-expressions).
ARAS
Aras,

If you plan to go with regex, then I highly recommend you to learn a bit more
about it. Here is one of the most well done tutorial on regex:

http://www.regular-expressions.info/tutorial.html

Otherwise, regex in LC are implemented via the PCRE library. ( check pcre.org )
Until now, this library is an old one in LC; therefore be careful with a lot of "not working regex".
In LC 6.5, the PCRE has been updated to the latest!

Considering that LC is a very verbose language, and regexs are certainly not,
put some sugar on some abrasive comments agaisnt regex, somehow 2 different worlds :)

And last,I'm sure you know regex or LC are tools and by no way *the* solution;
so use regexs only when you really need it. LC has some powerful commands and functions to
manage chunck of text.

Otherwise, talking of html, there are some pretty parsers library available on the net.
Probably worth having a look. This may save you some precious time..

Ok, so was my thoughts on a nice sunday morning, cold air but blue and sunny sky :)

Be happy,

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

ARAS
Posts: 55
Joined: Sat Nov 02, 2013 5:35 pm

Re: Find a word in a field and record its location

Post by ARAS » Sun Nov 03, 2013 11:08 am

Thanks Thierry,

Your words will be my guide. :o

Enjoy the sunny sky!
ARAS

Post Reply