Difficult find and replace problem (for me)

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Difficult find and replace problem (for me)

Post by richmond62 » Tue Jun 16, 2020 3:42 pm

Dunno about REGEX, but why bother:
-
Screenshot 2020-06-16 at 17.40.14.png
-
I couldn't understand any of your code, so I wrote my own. :?

Code: Select all

on mouseUp
   put 1 into LYNE
   put fld "fURTEXT" into URTEXT
   repeat until char 1 of URTEXT is empty
      if char 1 of URTEXT is "[" then
         put "START" into SEMAPHORE
      else
         if char 1 of URTEXT is "]" then
            put "FINISH" into SEMAPHORE
            put "]" after line LYNE of fld "fXTRACT"
            add 1 to LYNE
         end if
      end if
      if SEMAPHORE contains "START" then
         put char 1 of URTEXT after line LYNE of fld "fXTRACT"
      end if
      delete char 1 of URTEXT
   end repeat
end mouseUp
5 minutes. 8)
Attachments
GEORGE.livecode.zip
Here's the stack.
(1.29 KiB) Downloaded 185 times

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

Re: Difficult find and replace problem (for me)

Post by Thierry » Tue Jun 16, 2020 3:51 pm

marksmithhfx wrote:
Tue Jun 16, 2020 2:41 pm
Does anyone know how to convert the following to regex?

select all text between "[" and "]" including the brackets

e.g. select "[abcdef123]" or "[bbb]" from a line of text
Hi Mark,

as a start, not tested:

Code: Select all

  if matchText(T, "(?x) (\[ [^\] ]+ ] )", gotIt) then put gotIt
or

Code: Select all

  if matchText(T, "(\[[^\]]+])", gotIt) then put gotIt

If you want to iterate through your input text,
use replacetext, and the regex should be the same.

Kind regards,

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Difficult find and replace problem (for me)

Post by richmond62 » Tue Jun 16, 2020 4:20 pm

Hmm . . . REGEX does look a whole lot faster, IFF it works. 8)

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

Re: Difficult find and replace problem (for me)

Post by Thierry » Tue Jun 16, 2020 6:07 pm

richmond62 wrote:
Tue Jun 16, 2020 4:20 pm
Hmm . . . REGEX does look a whole lot faster
Ok, I did test it now :wink:

input text:
123[xcfdfdf]456[kjfklgdfgfkjg]789
123[4234234234]456[t45t4e453]789[34f343ff]0
result with replaceText:
123456789
1234567890
:roll:
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Difficult find and replace problem (for me)

Post by richmond62 » Tue Jun 16, 2020 8:45 pm

I have a feeling the OP wanted that sort of thing in reverse:
they wanted what was inside the [] rather than what was outside.

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: Difficult find and replace problem (for me)

Post by anmldr » Wed Jun 17, 2020 1:15 am

OP here. As a hypothetical...

What I was playing around with was something like a Find and Replace app primarily for when creating a website. But, it would be for other file types too. I don't have the ability to create an app like this anytime soon.

So, the app user would be prompted to select a folder. They would enter what they wanted to find and what they wanted to replace it with.

The app would then search through all files in the folder and do the Find/Replace in all files. As I thought this through, I remembered that the hyperlinks also may contain the string to be replaced and I wouldn't want that.

Like I wrote, it was all hypothetical. I was not building the app. I just remember a very old fantastic app on Windows called Advanced Find and Replace. They don't have an OS X version, just the Windows one. It was a very useful app for me "way back when" I used Windows.

Just for the "h" of it I decided to Google the app's name and here are some screenshots of the app. I guess he has kept it going over the years. Good for him!
https://www.abacre.com/afr/scrshots.htm

Thanks for the help.

Linda

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

Re: Difficult find and replace problem (for me)

Post by Thierry » Wed Jun 17, 2020 7:01 am

richmond62 wrote: I have a feeling the OP wanted what was inside the [] .
Then, you already have it in my 1st post.

Code: Select all

if matchText(T, "(?x) (\[ [^\] ]+ ] )", gotIt) then put gotIt
Best,

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

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

Re: Difficult find and replace problem (for me)

Post by Thierry » Wed Jun 17, 2020 8:49 am

Hi,

and finally,
using the same regex as the one in my 1st post,
that's how I would do it for a long text:

Code: Select all

 
   get sunnYextract( T, "(?x) (\[ [^\] ]+ ] )", "\1\n", resultText)
   put resultText
   
with T being:
123[xcfdfdf]456[kjfklgdfgfkjg]789
123[4234234234]456[t45t4e453]789[34f343ff]0
the resultText will be:
[xcfdfdf]
[kjfklgdfgfkjg]
[4234234234]
[t45t4e453]
[34f343ff]

Be well,

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7237
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Difficult find and replace problem (for me)

Post by jacque » Wed Jun 17, 2020 5:15 pm

I just remember a very old fantastic app on Windows called Advanced Find and Replace. They don't have an OS X version, just the Windows one. It was a very useful app for me "way back when" I used Windows.
BBEdit for Mac does that, among dozens of other things. I live in that app. There's a free version called Textwrangler.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Difficult find and replace problem (for me)

Post by Thierry » Wed Jun 17, 2020 5:22 pm

jacque wrote: There's a free version called Textwrangler.
Nowadays, BBEdit Free version is replacing textWrangler.

barebones BBEdit Free.png


https://www.barebones.com/products/textwrangler/


Regards,

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7237
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Difficult find and replace problem (for me)

Post by jacque » Wed Jun 17, 2020 6:13 pm

Thierry wrote:
Wed Jun 17, 2020 5:22 pm
Nowadays, BBEdit Free version is replacing textWrangler.
That's good to know, it was confusing to have different names. I can't say enough good things about BBEdit, it even has a LiveCode plugin to colorize and work with LC scripts.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Difficult find and replace problem (for me)

Post by richmond62 » Wed Jun 17, 2020 6:43 pm

jacque wrote:
Wed Jun 17, 2020 5:15 pm
There's a free version called Textwrangler.
Not any more there isn't.

"Note from the developers site: "We are sunsetting TextWrangler, and we encourage anyone interested in TextWrangler to download and use BBEdit instead."

https://www.barebones.com/products/textwrangler/

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

Re: Difficult find and replace problem (for me)

Post by Thierry » Wed Jun 17, 2020 6:50 pm

jacque wrote:
Wed Jun 17, 2020 6:13 pm
Thierry wrote:
Wed Jun 17, 2020 5:22 pm
Nowadays, BBEdit Free version is replacing textWrangler.
That's good to know
I can't say enough good things about BBEdit,
it even has a LiveCode plugin to colorize and work with LC scripts.
Sure, excellent tool with so much features...
It's my main development tool today, and their support is excellent.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Difficult find and replace problem (for me)

Post by marksmithhfx » Tue Jun 23, 2020 8:29 pm

Hi, sorry I was offline for so long. But many thanks for the profusion of replies.

Richmond, interesting. I was a bit surprised when I read:

if SEMAPHORE contains "START" then
put char 1 of URTEXT after line LYNE of fld "fXTRACT"
end if

But that was because you were "copying" the text between (and including) the brackets [ and ] and I was looking for the opposite, the text NOT in the brackets. But that's down to me since when I went back and re-read my message I was not clear at all.

Thiery, thank you so much for the solutions in regex. I should really try and invest some time in figuring out how it works since it is handy and powerful looking. I tried both your solutions and they both worked. Which does beg the question, what is the difference?

first ex: "(?x) (\[ [^\] ]+ ] )"
second ex: "(\[[^\]]+])"

I do have a couple of questions

1. Does the "(?x)" mean match the second (group) one or more times?
2. does the second group mean find the literal character "[" followed by some stuff "[^" followed by another literal character "]" one or more times?

Still looks greek to me. Here's my code below, includes my solution in LC plus the 2 regex solutions. Typical in/out was:

in: Taiwan[19]
out: Taiwan

The bit about the tcounter is just to organize a single column of input into 2 columns in the output ie. if even lines follow by cr, if odd lines follow by tab

Code: Select all

on mouseUp pButtonNumber
   put url ("file:" & specialfolderpath("desktop") & "/LC Drop File/capital.txt") into tText
   -- convert text file to a 2 column tab delimited list
   put 0 into tcounter
   repeat for each line tline in tText
      -- check for bracketed text like [this] in the string, and cut
      -- put replacetext(tline, "(?x) (\[ [^\] ]+ ] )", empty) into tline
      put replacetext(tline, "(\[[^\]]+])", empty) into tline
      --      put offset ("[", tline) into tStart
      --      if tStart > 0 then
      --         put offset ("]", tline) into tEnd
      --         if tEnd > tStart then
      --            put char 1 to tStart-1 of tline into tFirst
      --            put char tEnd + 1 to the number of chars of tline of tline into tLast
      --            put tFirst & tLast into tline
      --         end if
      --      end if      
      add 1 to tcounter
      if tcounter mod 2 is 0 then -- even number
         put tline & return after tList
      else -- odd number
         put tline & tab after tList
      end if
   end repeat
   set the dgText of group "DataGrid 1" to tList
end mouseUp
PS Thiery, any suggestions for brushing up on regex?

Cheers and many thanks everyone,
Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

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

Re: Difficult find and replace problem (for me)

Post by Thierry » Wed Jun 24, 2020 5:46 pm

Thierry, thank you so much for the solutions in regex.

I tried both your solutions and they both worked.
Mark, You're welcome.

what is the difference?
"(?x) (\[ [^\] ]+ ] )"
"(\[[^\]]+])"
Actually, there is none, except the 1st is easier to read - for the eyes.
(?x) set on the free-space mode

In free-spacing mode,
whitespace between regular expression tokens is ignored.
Whitespace includes spaces, tabs, and line breaks.
Note that only whitespace between tokens is ignored.
a b c is the same as abc in free-spacing mode.
But \ d and \d are not the same.
The former matches d, while the latter matches a digit.
\d is a single regex token composed of a backslash and a “d”.
Breaking up the token with a space gives you an escaped space
(which matches a space), and a literal “d”.

from: https://www.regular-expressions.info/freespacing.html

I do have a couple of questions

1. Does the "(?x)" mean match the second (group) one or more times?
2. does the second group mean find
the literal character "[" followed by some stuff "[^" followed
by another literal character "]" one or more times?

Still looks greek to me.
1. See above

2. find the literal "[" followed by any number of characters but NOT "]" and lastly "]"
the escaped \[ is mandatory because [ is a meta-character for regex.
the carret inside [ ] means NOT.
Here's my code below, includes my solution in LC
plus the 2 regex solutions.
I will give you 2 solutions in next post...

Regards,

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”