Problem with Regex Replacement

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
naugiedoggie
Posts: 1
Joined: Tue Jul 29, 2008 9:54 pm
Location: Naugatuck, CT USA
Contact:

Problem with Regex Replacement

Post by naugiedoggie »

Here is the regex find:

Code: Select all

GET /mydomain/MYHome.html \(.*\) \([0-9]+ .* .* .* .* .*www.google.com/search\?q=.* .*\)
Here is the replacement string:

Code: Select all

GET /mydomain/MYHome.html WT.mc_id=1234&\1 \2
Sample Target String:

Code: Select all

GET /mydomain/MYHome.html WT.vt_f_tlv=0&WT.tz=-5&WT.bh=2&WT.ul=en-us&WT.cd=32&WT.sr=1024x768&WT.jo=Yes&WT.ti=mydomain,%20Home%20of%20mydomain&WT.js=Yes&WT.jv=1.3&WT.ct=unknown&WT.bs=unknown&WT.fv=9.0&WT.slv=Not%20enabled&WT.tv=8.5.0&WT.dl=0&WT.ssl=0&WT.es=www.mydomain.com/mydomain/MYHome.html&WT.vt_f_tlh=0&WT.vt_f_d=1&WT.vt_f_s=1&WT.vt_f_a=1&WT.vt_f=1&WT.co_d=71.164.135.32-401865200.29948800&WT.vt_tlh=0&WT.co_a=71.164.135.32-401865200.29948800&WT.vt_tlv=0&WT.co=Yes&WT.vt_s=1&WT.vt_d=1&WT.vt_a_s=1&WT.vt_a_d=1&WT.vt_sid=71.164.135.32-399645200.29948800.1218438093747&WT.co_f=71.164.135.32-399645200.29948800 200 - - Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+6.0;+SLCC1;+.NET+CLR+2.0.50727;+Media+Center+PC+5.0;+.NET+CLR+3.0.04506;+MEGAUPLOAD+2.0) - http://www.google.com/search?q=mydomain&rls=com.microsoft:*:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7DKUS dcsi63sbk10000spqr4fuho2j_5f6t
Here is sample output in TextPad:

Code: Select all

GET /mydomain/MYHome.html WT.mc_id=1234GET /mydomain/MBHome.html WT.tz=-7&WT.bh=0&WT.ul=en-us&WT.cd=32&WT.sr=1280x800&WT.jo=Yes&WT.ti=mydomain%20Home%20of%20mydomain&WT.js=Yes&WT.jv=1.3&WT.ct=unknown&WT.bs=unknown&WT.fv=9.0&WT.slv=Not%20enabled&WT.tv=8.5.0&WT.dl=0&WT.ssl=0&WT.es=www.mbusa.com/mydomain/MBHome.html&WT.vt_f_tlh=1218438324&WT.vt_tlh=1218438326&WT.co=Yes&WT.vt_sid=68.229.21.141-2148327088.29948797.1218436980993&WT.co_f=68.229.21.141-2148327088.29948797 200 - - Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+6.0;+SLCC1;+.NET+CLR+2.0.50727;+Media+Center+PC+5.0;+.NET+CLR+3.0.04506) WEBTRENDS_ID=68.229.21.141-2153607088.29948797;+WT_ACCT=68.229.21.141-2153607088.29948797 http://www.google.com/search?q=mydomain&rls=com.microsoft:*:IE-Address&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7DKUS dcsi63sbk10000spqr4fuho2j_5f6tWT.tz=-7&WT.bh=0&WT.ul=en-us&WT.cd=32&WT.sr=1280x800&WT.jo=Yes&WT.ti=mydomain-%20Home%20of%20mydomain&WT.js=Yes&WT.jv=1.3&WT.ct=unknown&WT.bs=unknown&WT.fv=9.0&WT.slv=Not%20enabled&WT.tv=8.5.0&WT.dl=0&WT.ssl=0&WT.es=www.mbusa.com/mydomain/MBHome.html&WT.vt_f_tlh=1218438324&WT.vt_tlh=1218438326&WT.co=Yes&WT.vt_sid=68.229.21.141-2148327088.29948797.1218436980993&WT.co_f=68.229.21.141-2148327088.29948797 200 - - Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+6.0;+SLCC1;+.NET+CLR+2.0.50727;+Media+Center+PC+5.0;+.NET+CLR+3.0.04506) WEBTRENDS_ID=68.229.21.141-2153607088.29948797;+WT_ACCT=68.229.21.141-2153607088.29948797 http://www.google.com/search?q=mydomain&rls=com.microsoft:*:IE-Address&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7DKUS dcsi63sbk10000spqr4fuho2j_5f6t
Here is sample output from Regex Coach:

Code: Select all

GET /mydomain/MYHome.html WT.mc_id=1234&WT.vt_f_tlv=0&WT.tz=-5&WT.bh=2&WT.ul=en-us&WT.cd=32&WT.sr=1024x768&WT.jo=Yes&WT.ti=mydomain%20Home%20of%20mydomain&WT.js=Yes&WT.jv=1.3&WT.ct=unknown&WT.bs=unknown&WT.fv=9.0&WT.slv=Not%20enabled&WT.tv=8.5.0&WT.dl=0&WT.ssl=0&WT.es=www.mbusa.com/mydomain/MBHome.html&WT.vt_f_tlh=0&WT.vt_f_d=1&WT.vt_f_s=1&WT.vt_f_a=1&WT.vt_f=1&WT.co_d=71.164.135.32-401865200.29948800&WT.vt_tlh=0&WT.co_a=71.164.135.32-401865200.29948800&WT.vt_tlv=0&WT.co=Yes&WT.vt_s=1&WT.vt_d=1&WT.vt_a_s=1&WT.vt_a_d=1&WT.vt_sid=71.164.135.32-399645200.29948800.1218438093747&WT.co_f=71.164.135.32-399645200.29948800 200 - - Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+6.0;+SLCC1;+.NET+CLR+2.0.50727;+Media+Center+PC+5.0;+.NET+CLR+3.0.04506;+MEGAUPLOAD+2.0) - http://www.google.com/search?q=mydomain&rls=com.microsoft:*:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7DKUS dcsi63sbk10000spqr4fuho2j_5f6t
Regex Coach is producing the desired and expected behavior, based on my understanding of regex replacement. I am completely at a loss as to what is happening in TextPad. It appears that TP is interpreting the "\1" to be the entire beginning of the string, rather than the first capture string. Hopefully, someone can point out a fix.

Thanks.

mp
Do not neglect to do good and to share what you have. -- Hebrews 13:16a
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

The sample outputs you gave are not quite the outputs that were produced by TextPad or any other regex engine from the sample input.

But your problem is that in TextPad & in the replacement string means the same as \0, that is, whatever matched the entire regular expression. So you need to quote it: \& .

And you could more simply use
Find what: GET /mydomain/MYHome.html (.*[0-9]+.*www.google.com/search\?q=)
Replace with: GET /mydomain/MYHome.html WT.mc_id=1234\&\1

[X] Regular expression

Replace All
or similar.

This assumes Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Post Reply