On Mar 04, 2013, at 22:54, Scott <[email protected]> wrote:
> I am fascinated by the GREP capabilities of BBEdit and have been trying to 
> immerse myself in it.
> ...
> 
> Here's my latest problem.
> 
> Let's say I have a URL like this:
> 
> http://www.mysite.com/section/subsection/Z3245678a34/
> 
> And I want to turn it into an html link with the last element as the 
> highlighted text:
> 
> <a 
> href="http://www.mysite.com/section/subsection/Z3245678a34/";>Z3245678a34</a>
> 
> What would I use as the search & replace values?
______________________________________________________________________

Hey Scott,

GREP is wonderful stuff, but be prepared to bang your head against the wall 
more than a few times.  :)

A useful tool: http://gskinner.com/RegExr/

Speaking of which - I'd sure like to see BBEdit have a means to do live 
regex-match for prototyping patterns.

This pattern assumes your url is on one line and that the final backslash is at 
the end of the line.

   Find: http://.+?/([^/]+)(/)$
Replace: <a href="&">\1</a>

Parentheses represent captures.

Find:

'http://'  Literal string.
'.+?'      Any character, more than one, non-greedy.
'/'        Literal string.
'[^/]+'    Any character NOT '/'; more than one.
'/'        Literal string.
'$'        End of line.

Replace:

'&'        The entire matched string
'\1'       The 1st capture.

Another way to do it using positive lookahead assertion:

   Find: http://(.+?(?=/)/)+(.+?(?=/))(/)
Replace: <a href="&">\2</a>

You can find that on p. 179 of the BBEdit manual under Perl-Style Pattern 
Extensions.

BTW - now is a very good time for you to start writing text filters too.  You 
can save them and build up a library of examples.

I've create a default text filter and given it a keyboard shortcut of Ctrl-J.

I've also written a script to open that file in BBEdit and given it a keyboard 
shortcut of Ctrl-Opt-J.

This makes it very convenient to write and execute a text filter — more 
convenient in some respects than using the find/replace dialog.

-------------------------------------------------------------------------------------------
# PERL TEXT FILTER USING THE SAME REGEX AS THE FIRST EXAMPLE
-------------------------------------------------------------------------------------------

#! /usr/bin/env perl

use strict;

while (<>) {
        s!http://.+?/([^/]+)(/)$!<a href="$&">$1</a>!;
        print;
}

-------------------------------------------------------------------------------------------
# A SIMPLE FIND/REPLACE TEMPLATE
-------------------------------------------------------------------------------------------

#! /usr/bin/env perl

use strict;

while (<>) {
        # You can have multiple find/replace actions.
        s!FIND-PATTERN!REPLACE-PATTERN!;
        s!FIND-PATTERN!REPLACE-PATTERN!;
        print;
}

-------------------------------------------------------------------------------------------

Note that you can change the ! separator to other characters like '/', '#', or 
'~'.

Perl will only find the first match unless you tell it otherwise:

      s![a-z]+!•!gi;

     'g' = global
     'i' = case-insensitive

The default is a forward slash, but sometimes that's hard to read - especially 
when you have to escape a bunch of characters.

Note also that in Perl capture references use the dollar sign instead of the 
backslash.

Text filters are saved here:

~/Library/Application Support/BBEdit/Text Filters/

See the manual for more information.

--
Best Regards,
Chris

-- 
-- 
You received this message because you are subscribed to the 
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem, 
please email "[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to