Jr. RegExp Question

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Jr. RegExp Question

Don Stefani
Hello,

I'm trying to clear out a large number of variables from an old file
I'm modifying.
All of them have the format of   $FORM{'var_name'} .

I've been trying to use :s/ to get rid of them...

I can match a specific var if I write it literally in my regexp:
But I want to match like s/\$FORM{'\w*'}//g

Can you tell me where I'm going wrong?

Thanks,

dstefani
Reply | Threaded
Open this post in threaded view
|

Re: Jr. RegExp Question

Stephen R Laniel
On Wed, Aug 10, 2005 at 12:53:54PM -0700, Don Stefani wrote:
> I can match a specific var if I write it literally in my regexp:
> But I want to match like s/\$FORM{'\w*'}//g

First, I'd do minimal matching inside the ''. I'm sure
there's a way to use Perl-compatible regexes inside of vim,
but in any case you could do

s/\$FORM{'[^']'}//g;

Then you could replace FORM with 'Any characters that aren't
left braces or $' -- e.g.,

s/\$[^${]\+{'[^']'}//g

Then apply that to the whole document with

:%s/\$[^${]\+{'[^']'}//g

See if that does what you want.

--
Stephen R. Laniel
[hidden email]
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Jr. RegExp Question

Tim Chase-2
In reply to this post by Don Stefani
> All of them have the format of   $FORM{'var_name'} .
>
> I can match a specific var if I write it literally in my
> regexp: But I want to match like s/\$FORM{'\w*'}//g

I created a junk file with piles of lines containing

$FORM{'var_name'}

as you describe, and the regexp you gave deleted them all.  Good
ol' "it worked for me."(tm) :)

You might try

        :%s/\$FORM{'[^']*'}//g

Additionally, if you do want to use "\w*" then you might want to
check you setting for iskeyword:

        :set iskeyword?

to make sure it has the underscore in it (or includes the
characterset that will show up in such a find).  It might be
omitting the underscore (there by default but may have been
removed), unicode characters, periods, hyphens, etc.

Or are you one of the crazies that toggle the 'magic' setting? ;)

        :set magic?

Perhaps a broader sampling of "var_name" examples?  Are they
always single-quotes, or can they be double-quotes too (some
languages like PHP are rather flexible in this sense)?

You might also want to try turning on highlight searching and
then searching for your given pattern.  This should highlight
what Vim thinks should be deleted when that regexp is used in
the :s command.

Those are some of the first things I'd try.

-tim




Reply | Threaded
Open this post in threaded view
|

Re: Jr. RegExp Question

Don Stefani
Thanks for the input, my regexp, did not work, it told me pattern did not match.
When I added the \h before my \w it worked fine.

I'm curious why my original didn't work, yet it worked for you?

- dstefani



On 8/10/05, Tim Chase <[hidden email]> wrote:

> > All of them have the format of   $FORM{'var_name'} .
> >
> > I can match a specific var if I write it literally in my
> > regexp: But I want to match like s/\$FORM{'\w*'}//g
>
> I created a junk file with piles of lines containing
>
> $FORM{'var_name'}
>
> as you describe, and the regexp you gave deleted them all.  Good
> ol' "it worked for me."(tm) :)
>
> You might try
>
>         :%s/\$FORM{'[^']*'}//g
>
> Additionally, if you do want to use "\w*" then you might want to
> check you setting for iskeyword:
>
>         :set iskeyword?
>
> to make sure it has the underscore in it (or includes the
> characterset that will show up in such a find).  It might be
> omitting the underscore (there by default but may have been
> removed), unicode characters, periods, hyphens, etc.
>
> Or are you one of the crazies that toggle the 'magic' setting? ;)
>
>         :set magic?
>
> Perhaps a broader sampling of "var_name" examples?  Are they
> always single-quotes, or can they be double-quotes too (some
> languages like PHP are rather flexible in this sense)?
>
> You might also want to try turning on highlight searching and
> then searching for your given pattern.  This should highlight
> what Vim thinks should be deleted when that regexp is used in
> the :s command.
>
> Those are some of the first things I'd try.
>
> -tim
>
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Jr. RegExp Question

Charles E Campbell Jr
In reply to this post by Don Stefani
Quoting Don Stefani <[hidden email]>:

> Thanks, this worked perfectly.
> I was pretty close, help on \h doesn't say much and googling 'head of
> word character' doesn't either. What exactly is this doing? Err, why
> did it work?

> > :%s/\$FORM{'\h\w*'}//g

I think that your original pattern should work; I wonder if perhaps you
left off that leading ":%".  The leading "%" is the same as 1,$ ; ie.
the entire file.  If you happened to try your substitute on a line that
didn't have that $FORM{'...'} pattern, then the substitute would fail.

The \h stands for head-of-word; ie. any alphameric character (a-z, A-Z)
or an underscore.  For most languages, variable names don't begin with
digits (which \h won't match).

Regards,
Chip Campbell

Reply | Threaded
Open this post in threaded view
|

Re: Jr. RegExp Question

Stephen R Laniel
In reply to this post by Stephen R Laniel
On Wed, Aug 10, 2005 at 03:58:28PM -0400, Stephen R Laniel wrote:
> Then apply that to the whole document with
> :%s/\$[^${]\+{'[^']'}//g

Just for the historical record, that should have been

:%s/\$[^${]\+{'[^']\+'}//g

--
Stephen R. Laniel
[hidden email]
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key

signature.asc (196 bytes) Download Attachment