Page 1 of 1

Search hyphenated words

Posted: Tue Jun 23, 2015 8:23 am
by shalu
I am a beginner in live code. I have a Scrolling field that contain lots of text I want to seach the hyphenated words eg (usa-uk,usa--uk,usa---uk ) if these type of words present in the scrolling filed then it should place another one scrolling field. is it possible :oops:

Re: Search hyphenated words

Posted: Tue Jun 23, 2015 8:32 am
by Thierry
shalu wrote: I have a field that contain lots of text I want to seach the hyphenated words eg (usa-uk,usa--uk,usa---uk ) if these type of words present in the scrolling filed then it should place another one scrolling field.
Hi Shalu,

Here is a quick one for a start:

Code: Select all

   put "xxx, zzz, usa-uk,usa--uk,usa---uk,zzz" into T
   put replaceText( T, "usa-+uk", "USA_UK" )
You might find interesting to check in the dictionary:
replaceText(), matchChunk() and matchText().
They all works with regular expressions (regex)

Good luck,

Thierry

how regular expression are used in livecode

Posted: Tue Jun 23, 2015 10:28 am
by shalu
how regular expression are used in livecode. for eg I want to seacrch hyphenated words for that I am uses "\w\-\w" how I am uses this code

Re: how regular expression are used in livecode

Posted: Tue Jun 23, 2015 11:01 am
by shalu

Code: Select all

on mouseUp
   put the text of fld "SSS" into sha
   repeat for each word tLine in sha 
      put matchtext(tLine, "(\w\-\-\w)") into ssa
      if ssa is true then
         answer tLine
       
       answer ssa
          end if
   end repeat

end mouseUp

Re: Search hyphenated words

Posted: Tue Jun 23, 2015 11:55 am
by Klaus
Hi shalu,

beginner or not, do NOT open two threads for the same problem!
I merged these two threads!


Best

Klaus

Re: Search hyphenated words

Posted: Tue Jun 23, 2015 2:07 pm
by dunbarx
Hi.

Regex is the basis for the power behind replaceText(), matchChunk() and matchText(), as Thierry mentioned. He is our regex guru.

But am I missing your question? Did you simply want to replace all instances of words that contain "-" with another character? Or to eliminate that hyphen from the text entirely? If so. why not just:

Code: Select all

put the text of fld "SSS" into sha
replace "-" with "_" in sha
--or
replace "-" with empty in sha
That sort of thing.

Craig Newman

Re: Search hyphenated words

Posted: Tue Jun 23, 2015 3:21 pm
by FourthWorld
If you're using LiveCode 7 try the "trueWord" chunk type rather than "word". The "word" chunk type uses the old-style rules, maintained in v7 for backward compatibility. But with v7's inclusion of the comprehensive Unicode libraries for smarter parsing, the "trueWord" chunk type allows us to to accommodate more natural-language expressions.

For example, if you put this in a field:

Code: Select all

usa-CA, us-ca,something
...and then put this in a button:

Code: Select all

on mouseUp
   put the number of words of fld 1 \
         &cr& the number of trueWords of fld 1
end mouseUp
...you'll get:

2
5

Re: Search hyphenated words

Posted: Tue Jun 23, 2015 7:31 pm
by phaworth
I think what shalu wants to do is put the hyphenated words into a different field, not sure if they need to be deleted from the original field or not.

Is that correct shalu?

Pete

Re: Search hyphenated words

Posted: Tue Jun 23, 2015 9:37 pm
by dunbarx
If what Pete is saying is true, then the problem goes away. Put this into a button script, with field 1 the field of interest, and a fld 2 to hold the extracted data:

Code: Select all

on mouseUp
   set the itemdel to "-"
   get fld 1
   repeat with y = 1 to the number of items of it
      put the last word of item y of it & "-" & the first word of item y + 1 of it & return after temp
   end repeat
   put temp into fld 2
end mouseUp
But I suspect there is more to this...

Craig