Page 1 of 2

Regex help?

Posted: Fri Mar 07, 2014 7:46 am
by trenatos
I'm trying to figure out why this doesn't work.

put replaceText(convText, "\$([a-z]*)=(\"[a-z]*\");", "ding")

I'm getting this error: compilation error at line 6 (Function: separator is not a ',') near "[", char 30

It doesn't seem to want to escape the " with \ and I don't know why, everything I've read says that's how to do it..

Re: Regex help?

Posted: Fri Mar 07, 2014 7:53 am
by Thierry
trenatos wrote:I'm trying to figure out why this doesn't work.
everything I've read says that's how to do it..
Hi tetranos,

You didn't read one of my old post :)

Your regex looks good!
The problem is LC compiler which is not happy with some backslash :(

Put your regex in a custom prop or in a field, and it should work.
(I didn't check it..)

regards,

Thierry

Re: Regex help?

Posted: Fri Mar 07, 2014 7:58 am
by trenatos
Hi Thierry,

You mean like this? put replaceText(convText, field "regField", "ding")

It's not working either, though no errors.

The issue is the LC compiler? :/

I'll sound like an ass but.. shouldn't something like that work in version 6.x? I mean, escaping characters is kind of common in regex.

Re: Regex help?

Posted: Fri Mar 07, 2014 8:11 am
by Thierry
trenatos wrote:Hi Thierry,
You mean like this? put replaceText(convText, field "regField", "ding")
Yes.
trenatos wrote: It's not working either, though no errors.
IF you like you can send me some text you want to match..
trenatos wrote:The issue is the LC compiler? :/

I'll sound like an ass but.. shouldn't something like that work in version 6.x? I mean,
escaping characters is kind of common in regex.
That's nothing to do with regex; try this:

Code: Select all

   put "1234" into x
   put "12\z34" into x
   put "12\"34" into x
Thierry

Re: Regex help?

Posted: Fri Mar 07, 2014 8:17 am
by trenatos
Sure, thanks for taking a look :)

$color="red";

Grabs everything except the $ and ; for the replace (Dynamic interpretation of PHP variables)

Sorry I should have clarified, I'm not blaming the Regex implementation but the compiler.

I've had other issues with both the compiler and IDE (Like Undo not working, tabs becoming "stuck", slowing down to syrup over time, etc.)

When LC works, it's absolutely GREAT, seriously, but the "little quirks" is starting to get to me.

Re: Regex help?

Posted: Fri Mar 07, 2014 8:34 am
by trenatos
Actually, even this should work at least for testing: put replaceText(convText,"\$(.*);", "blabla")

But nope. It doesn't throw an error, but it also doesn't match $color="red"; in the source text.

Re: Regex help?

Posted: Fri Mar 07, 2014 8:47 am
by Thierry
trenatos wrote: $color="red";
Grabs everything except the $ and ; for the replace (Dynamic interpretation of PHP variables)

Code: Select all

fld 1: \$[a-z]*=\"[a-z]*\";
 put replaceText( fld 2, fld 1, "$ding;")
Using the parens, means you can capture the pattern find inside a pair of parens.
Unfortunately, this is useless with the replaceText() LC function.
Check in LC Engine forum to see what I wanted to implement :)

Back to your example, you could write something like that:

Code: Select all

fld 1: \$([a-z]*=\"[a-z]*\");
get matchText( fld 2, fld 1, gotIt)
get matchChunck( fld 2, fld 1, gotIt1,gotIt2)
Does this make sense?

Thierry

Re: Regex help?

Posted: Fri Mar 07, 2014 9:01 am
by trenatos
Your first example replaced the string with $ding, but you're saying that () won't work.

And I don't understand how the second example would let me replace the text with part of the matched text.

Basically, I need to convert $color="red"; to <cfset color="red"> but dynamically, it should work with unknown php variables in the source text.

Seems that matchChunk only lets you know if the match exists (Returns only true/false)

Re: Regex help?

Posted: Fri Mar 07, 2014 9:24 am
by Thierry
trenatos wrote:Your first example replaced the string with $ding, but you're saying that () won't work.
Well, there is no parens in the regex?
So, it's me who is confused.
trenatos wrote: And I don't understand how the second example would let me replace the text with part of the matched text.
Sorry. As I don't know your skills, it's not easy to give specific answers; it's a wilde area..
matchChunk() or matchText() puts the results pattern in variable(s) after the regex.
The LC coder has to do something with it afterwards.

replaceText() does the replacement automatically in your original text, but you can't use the
capture parens.
trenatos wrote:Basically, I need to convert $color="red"; to <cfset color="red"> but dynamically, it should work with unknown php variables in the source text.
I'll come back in a couple of hours with 3-4 lines of code for this..
too busy right now.
trenatos wrote:Seems that matchChunk only lets you know if the match exists (Returns only true/false)
Yes, and if it is true, then you work with the variables I spoke above.

Thierry

Re: Regex help?

Posted: Fri Mar 07, 2014 9:45 am
by trenatos
Live and learn, ok so gotIt contains the string match, gotIt1 and gotIt2 are the positions of the first and last characters of the matched string.

convText contains a block of php code.

Code: Select all

   get matchChunk( convText, field "regField", gotIt1,gotIt2)
   put "\$" & gotIt & ";" into changeMe
   put replaceText( convText, changeMe, "<cfset " & gotIt & ">")
The above code finds and replaces the $color="red" within a block of text, but it of course fails with subsequent php variables, but it's a start!

Re: Regex help?

Posted: Fri Mar 07, 2014 9:54 am
by Thierry
trenatos wrote:Live and learn,
:)

and here a working solution with some useless extra lines to learn quicker.
well, hope so...

Code: Select all

on mouseUp
   local x
   put fld 2 into T
   -- fld "Regex: (?msi)(\$(\w+=\"\w+\");)
   repeat while matchChunk( T, fld "Regex", p1start,p1End,p2Start,p2End)
      put "p1start-p1End: " & char p1Start to p1End of T &cr after x
      put "p2start-p2End: " & char p2Start to p2End of T &cr after x
      get format( "<cfset %s>",char p2Start to p2End of T)
      put "new pattern: " & IT  &cr after x
      put IT into char p1Start to p1End of T
      if the controlkey is down then exit repeat
   end repeat
   put x &cr& T
end mouseUp
Thierry

Re: Regex help?

Posted: Fri Mar 07, 2014 10:01 am
by Thierry
and following my idea from http://forums.runrev.com/viewtopic.php?f=66&t=17412

your solution could have been:

Code: Select all

replaceText( aText,  fld "Regex", "<cfset \2>") 
Regards,

Thierry

Re: Regex help?

Posted: Fri Mar 07, 2014 10:04 am
by trenatos
Almost ;)

Your function actually replaces the variable $color="red"; with this: $<cfset >;

--edit--

waaaait, I forgot to chang the field to the regex in your comment, it totally works now, you have no clue how much that saved my sanity, I can actually continue this project idea now.

I'm curious about your 1-line solution though, have to try that out too :D

Re: Regex help?

Posted: Fri Mar 07, 2014 10:06 am
by Thierry
trenatos wrote:Almost ;)

Your function actually replaces the variable $color="red"; with this: $<cfset >;
Not here ??

Are you using my latest regex ( put in comments in teh previous script) ?

Thierry

Re: Regex help?

Posted: Fri Mar 07, 2014 10:08 am
by trenatos
Sorry, yes, I edited my last post to reflect that I finally saw your regex in the comment