Go Back   FlashFXP Forums > >

Project: FlashFXP Bug Reports Ticket Tools
ID: 1025 Category: FlashFXP Bug
Title: pcre does not work in $replacerx Status: Closed (Fixed / Implemented)
Severity: Minor Version: 5.0.0

Senior Member
DayCuts
03-16-2015, 03:52 AM
pcre does not work in $replacerx

Only just noticed this so not sure when it popped up but it is present in at least 3815-3817

Using pcre in $replacerx() does not work. Noticed this while attempting to remove a trailing / with $replacerx("%p","rx: /$","") and subsequent testing confirmed that any pcre fails. I tried several formats of the command with/without the quotes around <find> field, with/without the rx: prefix, combinations of both, etc. I also tried simply replacing a single alphanumeric character to no avail.
FlashFXP Developer
bigstar
03-16-2015, 11:32 PM
Re: pcre does not work in $replacerx

I will need to test older builds to determine if this behavior has changed, but I don't think it has.

I just noticed that the help file on this command very wrong and doesn't explain how this command works.

$replacerx("<input>", "<pattern>", "<replacement>")

Code:
/echo $replacerx("/this/is/my/end/game/", "(.*)/$", "\1")
Output:
[CMD] /this/is/my/end/game

all text up until the last / is stored in token \1 matching (.*)
Senior Member
DayCuts
03-17-2015, 06:38 AM
Re: pcre does not work in $replacerx

Ah, the help file suggests it works in the same manner as $replace. (which would be more naturally expected)

Your example above suggests that $replacerx() is actually a general regular expression string manipulation rather than a substitution algorithm. Since pcre doesn't actually include a native function for substitution making $replacerx() work in the same manner as $replace would require manipulation using the regmatch_t array argument of regexec (and memory manipulation) or possible pcrs extention (which I am not familiar with but is available from pcre.org).

Although it would make writing the expressions in $replacerx() more natural (and simpler) it's possibly not worth the effort to implement.

Adjusting the modifier description and a good example should probably suffice to dispel the assumption that is works like $replace(). As you say the argument names are not very accurate so <replacement> should actually be <output> or similar.
FlashFXP Developer
bigstar
03-17-2015, 10:53 AM
Re: pcre does not work in $replacerx

I reviewed the source code and it appears that this is how the token modifier has always behaved.

After conducting some more tests and doing a fair bit of research it appears that some of the functionality provided by this function is not correct. Our function should (or at-least I thought it did) provide results similar to that of preg-replace() in PHP (however it doesn't)

The following example returns the wrong result, when the replacement is blank the pattern should be removed from the result.
Code:
/echo $replacerx("/this/is/my/path/", "/$", "")
Instead we get a result of
[CMD] /this/is/my/path/
(This is because on PCRE failure or error the original string is returned as the result)

The code wasn't handling an empty replacement very well and it resulted in a failure.

I have corrected this for the next release.

Taking a slightly modified example from the preg-replace PHP webpage the following code would replace brown with black but it doesn't.
Code:
/echo $replacerx("The quick brown fox jumped over the lazy dog.", "brown", "black")
[CMD] black

Instead we get a result of black, I'm not sure what's going on here and I will need to investigate this issue further.

If we modify the pattern and replacement to use back references then everything works as expected.
Code:
/echo $replacerx("The quick brown fox jumped over the lazy dog.", "(.*)brown(.*)", "\1black\2")
I will need to investigate this further to fully understand why it doesn't work, its possible that its a mistake in my code or a limitation in the 3rd party helper functions I'm using.
FlashFXP Developer
bigstar
03-17-2015, 06:34 PM
Re: pcre does not work in $replacerx

The partial fix I mentioned above has been implemented in FlashFXP v5.1 build 3818

The secondary issue I discovered with trying to replace brown with black is still under investigation and hopefully it can be resolved in a future build.
FlashFXP Developer
bigstar
03-17-2015, 09:14 PM
Re: pcre does not work in $replacerx

Okay I think I got everything fixed and working correctly now, below are the tests that I performed.

Code:
/echo $replacerx("/my/path/test/", "/$", "")
[CMD] /my/path/test

Code:
/echo $replacerx("The quick brown fox jumped over the lazy dog.", "brown", "black")
[CMD] The quick black fox jumped over the lazy dog.

Code:
/echo $replacerx("This job is 65% complete.", "\d+", "100")
[CMD] This job is 100% complete.

Code:
/echo $replacerx("FlashFXP51_3818_Setup.exe", "^(\w+\d+)_(\d+)_(.*)$","\1_\3")
[CMD] FlashFXP51_Setup.exe

Code:
/echo $replacerx("The quick brown fox jumped over the lazy dog.", ".+", "\i0")
[CMD] The Quick Brown Fox Jumped Over The Lazy Dog.

Code:
/echo $replacerx("The quick brown fox jumped over the lazy dog.", ".+", "\l0")
[CMD] the quick brown fox jumped over the lazy dog.

Code:
/echo $replacerx("The quick brown fox jumped over the lazy dog.", ".+", "\U0")
[CMD] THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.

.... And here's an updated build with the changes
Download ffxp5.1.0.3819.zip
Senior Member
DayCuts
03-17-2015, 10:16 PM
Re: pcre does not work in $replacerx

Just out of curiosity what library are you using and what was the problem? When you mentioned that capturing groups / referencing corrected that example it again led me to the difference between a normal string manipulation function and a substitution function but your comments suggest that may not be the case.
FlashFXP Developer
bigstar
03-18-2015, 07:24 AM
Re: pcre does not work in $replacerx

For regex replace I'm using TPerlRegEx from http://www.regular-expressions.info/delphi.html

I have a helper function that interfaces with the TPerlRegEx object class and my helper contained a flaw which in turned caused certain operations to fail.
Ticket Tools
Subscribe to this Ticket


Posting Rules
You may not post new tickets

Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT -5. The time now is 12:16 PM.

Parts of this site powered by vBulletin Mods & Addons from DragonByte Technologies Ltd. (Details)