Copy link to clipboard
Copied
Hi guys,
I have a working grep for finding url in an InDesign document and I'd say it finds everything:
app.findGrepPreferences.findWhat="(http|https|Http|Https|rtsp|Rtsp|ftp|www).+?(?=(\\)|\\.|\\,|\\?|\\!|\"|'|;|\\r)*(\\s))";
the only problem is can't find the ones at the end of the story.
Any thoughts?
Thanks,
-CharlesD
(http|https|Http|Https|rtsp|Rtsp|ftp|www).+?(?=(\\)|\\.|\\,|\\?|\\!|\"|'|;|\\r)*($|\\s)) seems to work.
Copy link to clipboard
Copied
I hope you are using app.changeGrep(); to change.. if not use this to change for entire document
Copy link to clipboard
Copied
Hi,
var myDoc = app.activeDocument;
var Stories = app.activeDocument.stories.everyItem().getElements()
for(i=0; i<Stories.length; i++)
{
app.findGrepPreferences = app.changeGrepPreferences=null;
app.findGrepPreferences.findWhat= ""; // find code
app.findGrepPreferences.changeTo= ""; // change code
app.changeGrep();
}
Thanks,
Prabu
Copy link to clipboard
Copied
stab in the dark, as i can't test:
(http|https|Http|Https|rtsp|Rtsp|ftp|www).+?(?=(\\)|\\.|\\,|\\?|\\!|\"|'|;|\\r)*(\\s|\\Z))
Copy link to clipboard
Copied
that's exactly how I tried it but '\\z' is a bit of a mystery. Any other options?
Copy link to clipboard
Copied
It should be capital - Z (not z)
Copy link to clipboard
Copied
I tried both, no luck
Copy link to clipboard
Copied
(http|https|Http|Https|rtsp|Rtsp|ftp|www)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/#-]*[\w@?^=%&/#-])?
Copy link to clipboard
Copied
(http|https|Http|Https|rtsp|Rtsp|ftp|www).+?(?=(\\)|\\.|\\,|\\?|\\!|\"|'|;|\\r)*($|\\s)) seems to work.
Copy link to clipboard
Copied
Perfect! I've seen a lot of grep combinations from this forum even from 5 years ago and i always check for new ideas but this one is by far the most reliable. please try it out and let me know its faults so we can improve it for everyone's benefit.
Copy link to clipboard
Copied
Hi,
Have you just tried it in a para with 2 www.….com?
(^/)
Copy link to clipboard
Copied
Actually, I think the absolute best one I've found for urls is: Daring Fireball: An Improved Liberal, Accurate Regex Pattern for Matching URLs
(short version:
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])) |
)
Copy link to clipboard
Copied
I've just copied/pasted it: It doesn't work!
(^/)
Copy link to clipboard
Copied
Vamitul, the one you posted from Daring Fireball failed on this url:
http://uli.org/wp-content/uploads/ULIDocuments/Norfolk_PanelReport_Fweb.pdf
It only gets up to this:
http://uli.org/wp-content/uploads/
If you guys have other patterns that you think is perfect please post it here.
It's time to establish the most reliable pattern once and for all. Should we create a new discussion?
Copy link to clipboard
Copied
Hi,
Please try this:
(https?://?|www?)[-./\~\w&]+
Thanks,
Prabu G
Copy link to clipboard
Copied
Thanks for the quick replies. I don't actually use changeGrep because I don't change anything I just have to find those urls and make them as live links. Here are some parts of my code:
app.findGrepPreferences.findWhat="(http|https|Http|Https|rtsp|Rtsp|ftp|www).+?(?=(\\)|\\.|\\,|\\?|\\!|\"|'|;|\\r)*(\\s))";
if (myDoc.findGrep().length > 0){
URLlinker(myDoc.findGrep());
}
function URLlinker(){
... do something with with the found urls
}
It finds all the urls except the end of the stories.
I also tried using \\s|\\z but it doesn't find anything. So I had to duplicate the function and just replace '\\s' with '\\z' but it gives me some odd results.
Copy link to clipboard
Copied
Hi,
I tried these both code but not working, can anyone help to find the problem?
var myDoc = app.activeDocument;
app.findGrepPreferences.findWhat="(https?://?|www?)[-./\~\w&]+";
if (myDoc.findGrep().length > 0){
URLlinker(myDoc.findGrep());
}
function URLlinker(){
app.changeGrepPreferences.appliedCharacterStyle="Hyperlink"
app.changeGrep();
}
****************************
var myDoc = app.activeDocument;
var Stories = app.activeDocument.stories.everyItem().getElements()
for(i=0; i<Stories.length; i++)
{
app.findGrepPreferences = app.changeGrepPreferences=null;
app.findGrepPreferences.findWhat= "(https?://?|www?)[-./~w&]+"; // find code
app.changeGrepPreferences.appliedCharacterStyle="Hyperlink"
app.changeGrep();
}
Thanks in advance.
by
hasvi
Copy link to clipboard
Copied
Ok guys I started a new thread:
[CC][JS]Searching for the most reliable URL grep pattern
Please post there your ideas. Thanks!