You are here: PSPad forum > English discussion forum > Re: Replacement strings help

Re: Replacement strings help

#1 Replacement strings help

Posted by: theplanetfixer | Date: 2013-11-08 15:38 | IP: IP Logged

I have a big file and I need to replace all underscore by dashes.

Like:

from:

Quote:
http://rogerpilon.com/pierre/2009/02/enfin_un_formulaire_dinscription.html

to

Quote:
http://rogerpilon.com/pierre/2009/02/enfin-un-formulaire-dinscription.html

I read the search and replace strings list but I admit, it is out of my league.

Could someone be kind enough to post the search/replace stings?

Thanks in advance!

Roger Pilonsmiling smiley

Edited 1 time(s). Last edit at 2013-11-08 15:39 by theplanetfixer.

Options: Reply | Quote | Up ^


#2 Re: Replacement strings help

Posted by: vbr | Date: 2013-11-08 16:46 | IP: IP Logged

theplanetfixer:
I have a big file and I need to replace all underscore by dashes.

Like:

from:

Quote:
http://rogerpilon.com/pierre/2009/02/enfin_un_formulaire_dinscription.html

to

Quote:
http://rogerpilon.com/pierre/2009/02/enfin-un-formulaire-dinscription.html

I read the search and replace strings list but I admit, it is out of my league.

Could someone be kind enough to post the search/replace stings?

Thanks in advance!

Roger Pilonsmiling smiley

Hi,
is there some special requirement for this replacement, or should simply all underscores be replaced by dashes?

in that case, you just call the replacement dialog: Ctrl+H (Menu: Search - Replace)
and fill in the pattern
Search:
_
Replace:
-
and press [Ok]

Or are you seeing some unexpected behaviour with this straightforward approach?

The more complex syntax for searches you found might be regular expressions, which wouldn't be needed here.

hth,
vbr

Options: Reply | Quote | Up ^


#3 Re: Replacement strings help

Posted by: theplanetfixer | Date: 2013-11-08 17:30 | IP: IP Logged

Thanks first for your fast answer.

To avoid messing around I would need something like this:

Any file starting by <<[url]http://>>[/url];
any file ending by <<.html>>
replace all underscore by dashes (between the http:// and .html)

Thanks a lot!

Roger

Options: Reply | Quote | Up ^


#4 Re: Replacement strings help

Posted by: Stefan | Date: 2013-11-08 18:24 | IP: IP Logged

theplanetfixer:
Thanks first for your fast answer.

To avoid messing around I would need something like this:

Any file starting by <<[url]http://>>[/url];
any file ending by <<.html>>
replace all underscore by dashes (between the http:// and .html)

Thanks a lot!

Roger

Do you mean: "any LINE starting..., any LINE ending..." ?

I asuming, the amount of occurrences of underscores are different for different lines?

And we don't can just search and replace over the whole file, because there are other occurrence of underscores in other lines too?

Then we will need a script.
Can we at least asume that those URLs are on one line only (no line break inbetween)?

.

--
Stefan (pleased user since 1722)
Do you know you can spend Jan a beer? (click here)
Inofficial FAQs + Infos + Special Settings

Options: Reply | Quote | Up ^


#5 Re: Replacement strings help

Posted by: vbr | Date: 2013-11-08 22:08 | IP: IP Logged

theplanetfixer:
Thanks first for your fast answer.

To avoid messing around I would need something like this:

Any file starting by <<[url]http://>>[/url];
any file ending by <<.html>>
replace all underscore by dashes (between the http:// and .html)

Thanks a lot!

Roger

Thanks for the clarification,
I'm assuming you have these URLs in one text file (having to scan multiple files would make this more complicated).
If you need to do this with replacement only (without scripting), I can only see a slightly inconvenient way - performing the same replacement repeatedly, until there are no matches left.
search pattern could be:
(http://.*)_(.*\.html)
replacement pattern:
$1-$2

[x] Regular expression search must be checked

Each run of the replacement handles one (the last one) underscore within each substring between http:// and .html (within a single line).

hth,
vbr

Edited 1 time(s). Last edit at 2013-11-08 22:09 by vbr.

Options: Reply | Quote | Up ^


#6 Re: Replacement strings help

Posted by: Stefan | Date: 2013-11-09 11:15 | IP: IP Logged

I thought more on something like this:

FROM:

Quote:
http //rogerpilon.com/pierre/2009/02/enfin_un_formulaire_dinscription.html more_text_here

http //rogerpilon.com/pierre/2009/02/
enfin_un_formulaire_dinscription.html more_text_here

I read the_search_and replace_strings list but I admit, it is out of my league.

Could_someone be kind enough_to post_the search/replace stings?

.

.

TO:

Quote:
http //rogerpilon.com/pierre/2009/02/enfin-un-formulaire-dinscription.html more_text_here

http //rogerpilon.com/pierre/2009/02/
enfin-un-formulaire-dinscription.html more_text_here

I read the_search_and replace_strings list but I admit, it is out of my league.

Could_someone be kind enough_to post_the search/replace stings?

That's it: replacing underscores _ in URL only with hyphens -

.

.

.

USE a script like this:
(make COPIES of your files first!)


module_name = "Stefan20131109001";
module_ver = "0.001";
function Init(){addMenuItem("Replace _ with - in URLs", "STEFAN JS sample", "doit");}

function doit() {
var myObject = newEditor();
myObject.assignActiveEditor();
myText = myObject.Text();

//join all lines:
worktext = myText.replace(/\n/g,"000myLineBreak000");
//split lines at HTTP and HTML:
worktext = worktext.replace(/http/ig,"\nhttp");
worktext = worktext.replace(/html/ig,"html\n");

//work on each line which contains HTTP:
out="";
LinesArray = worktext.split("\n");

for(x=0,L=LinesArray.length; x<L; x++){
line = LinesArray[x];
result = line.search(/http/);
if (result != -1) {
out += line.replace(/_/g, "-");
}else{
out += line;
}
}

//join all lines:
out = out.replace(/\n/g);
//break at origin line breaks:
out = out.replace(/000myLineBreak000/g, "\n");

//Output:
myObject.text(out);
//myObject.closeFile();
}

.

.

.

EDIT:

I was not able to format my code correctly sad smiley It's a horrible forum software.

module_name = "Stefan20131109001";
module_ver = "0.001";
function Init(){addMenuItem("Replace _ with - in URLs", "STEFAN JS sample", "doit");}

function doit() {
...var myObject = newEditor();
...myObject.assignActiveEditor();
...//myObject.NewFile();
...myText = myObject.Text();
...//echo(myText);
...
...//join all lines:
...worktext = myText.replace(/\n/g,"000myLineBreak000");
...//split lines at HTTP and HTML:
...worktext = worktext.replace(/http/ig,"\nhttp");
...worktext = worktext.replace(/html/ig,"html\n");
...
...//work on each line which contains HTTP:
...out="";
...LinesArray = worktext.split("\n");
...
...for(x=0,L=LinesArray.length; x<L; x++){
......line = LinesArray[x];
......result = line.search(/http/);
......if (result != -1) {
......... out += line.replace(/_/g, "-");
......}else{
......... out += line;
... }
...}
...
...//join all lines:
...out = out.replace(/\n/g);
...//break at origin line breaks:
...out = out.replace(/000myLineBreak000/g, "\n");
...
...//Output:
...//echo('out: '+out);
...myObject.text(out);
...//myObject.closeFile();
}

.

.

--
Stefan (pleased user since 1722)
Do you know you can spend Jan a beer? (click here)
Inofficial FAQs + Infos + Special Settings

Edited 6 time(s). Last edit at 2013-11-10 12:20 by Stefan.

Options: Reply | Quote | Up ^






Editor PSPad - freeware editor, © 2001 - 2024 Jan Fiala, Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák, Privacy policy and GDPR