How to substitute (lower) case in a pattern like in perl-ish example

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

How to substitute (lower) case in a pattern like in perl-ish example

L. A. Walsh

Is there an 'easy' (:-)) (I could write a program, but not exactly my
idea of an easy editing task)...still might end up being the fastest
way to do this...) way to change case of a 'bunch' of identifiers
delimited by curly brackets in a 'block' of text?

I.e. I have a module that currently uses variables of the form:

        pkg{VARA}=pkg2{VAR_B}+$offsetp->{VC};

The part that I want to change is the 'upper' case tag in curly
brackets to lower case.  So what I'd like is something like a
combination substitute and "tr" (character translate), so I can
do (spaces added for clarity, and using a perl-ish syntax, cuz if I
knew the vim syntax, I'd just use it :-) ):

: '<,'> s / { ([A-Z_]\+) } / { (? { $1=~tr/[A-Z]/[a-z]/ } ) } /xg <- vimish
:<range>s / L (capture ) L / L (? { evaluate-able expr. } ) L     <- legend

where - the parens in the first part captures the sub expression;
- above the 'L' in the legend are literal curly braces, delimiting
   the captured expression in the search-string, and added back as
   literals to the replacement string.
- and everything in (?{replacement-expr}) with the evaluated contents
   of a "tr" (translate) substitution operator that operates on
   the left-variable of the "=~" expression -- so "tr/[A-Z]/[a-z]/"
   would replace every char in the captured-sub-expression (represented
   by "$1") that matches in the 1st range "[A-Z]" to it's corresponding
   character in the target range "[a-z]" and return the result as the
   contents of the replacement sub-expression "(?{$1=~tr/[A-Z]/[a-z]/})",
   which would itself be between the two literal curly brackets in the
   replacement string.

==== that's "it" ====

Equivalently, if I could run the same command in perl (I have the
perl5.8.dll, but it doesn't seem to be integrated in a way to
do this:
: '<,'>  <-- starting with a vi "Visual" range, and appending a perl
command to operate on the range like:
: '<,'> perl 's/ {([A-Z_]+)} / { (?{$1=~tr/A-Z/[a-z]/}) } /xg' <- vimish
              ' <-- perl expression between single quotes --> ' <- legend

So what would be the equivalent line of vim-code to do what
I want above?  I happen to know the perlish way to do it (or at
least I think I do -- haven't tried the above to be honest, but it
was more intended to communicate what I want than being a working
perl example :-), mostly because I know the POSIX (*nix) search and
command set (I believe 'tr' the utility is also part of the POSIX
system command set, and perl was originally designed as an amalgamation
of of several commands that have ended up being part of the POSIX
command set.

Only problem with perl is it didn't include the 'vi[m]' editor :-).
I'm sure 'vim' would somehow be a great feature addition
in perl 5.12. (Perl6, being a fairly different language from its
predecessors, doesn't appear to fulfill the mission of being the
next version or evolution of perl5.8 or 5.10. Apparently the author
admits he only chose the name "perl6" for the perl-name recognition,
not because it was intended to be a compatible upgrade, operators and
variable syntax have even changed.)

BTW, is there a way (or a reason why the ability was disabled) to
always use "very magic" for interactive regular expressions?  Sure
would help me in remembering more of the expression syntax.  I never
know if I need an extra "\" to make something have it be treated
as its 'normal' Regex character function (magic) vs. being treated
as a literal without trying to look it up in a chart -- and even
then it's confusing with things like square-brackets not being
symmetric in their backslash requirements.  I understand the logic
of why it is the way it is, but that doesn't mean it's not a
different logic and mind-set from where 'symmetry' would take
precedence over necessity.


Thanks for any help & pointers...
-Linda





--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to substitute (lower) case in a pattern like in perl-ish example

Tim Chase

> Is there an 'easy' (:-)) (I could write a program, but not
> exactly my idea of an easy editing task)...still might end up
> being the fastest way to do this...) way to change case of a
> 'bunch' of identifiers delimited by curly brackets in a
> 'block' of text?
>
> I.e. I have a module that currently uses variables of the
> form:
>
>         pkg{VARA}=pkg2{VAR_B}+$offsetp->{VC};
>
> The part that I want to change is the 'upper' case tag in
> curly brackets to lower case. So what I'd like is something
> like a combination substitute and "tr" (character translate),
> so I can do (spaces added for clarity, and using a perl-ish
> syntax, cuz if I knew the vim syntax, I'd just use it :-) ):
>
> : '<,'> s / { ([A-Z_]\+) } / { (? { $1=~tr/[A-Z]/[a-z]/ } ) } /xg <- vimish
> :<range>s / L (capture ) L / L (? { evaluate-able expr. } ) L     <- legend

sounds like you're reaching for the \L replacement modifier:

   :'<,'>s/{[A-Z_]\+}/\L/g

which you can read about at

   :help s/\L

If you start experimenting with it, it's helpful to also know the
following

   \l
   \L
   \u
   \U
   \e / \E  (same thing to end a translation-run)


> BTW, is there a way (or a reason why the ability was disabled)
> to always use "very magic" for interactive regular
> expressions? Sure would help me in remembering more of the
> expression syntax. I never know if I need an extra "\" to make
> something have it be treated as its 'normal' Regex character
> function (magic) vs. being treated as a literal without trying
> to look it up in a chart

I have similar mental-taxation going the other way...from Vim to
(in my case) Python or sed syntax.  As with many, many, many
other things, vim has an option for that.  You can just

   set magic

in your vimrc and it should alleviate some of your problems
(though may cause portability problems as detailed at

   :help 'magic'

as some scripts have regexps that expect magic to be unset).

You can also use "\v" or "\m" in your search expression to
control it on a per-expression basis:

   :help /magic

I don't think there's a 'verymagic' setting that can be set
globally, so get verymagic, you have to use the "\v" (which I've
seen several folks here on the list use regularly).

-tim



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to substitute (lower) case in a pattern like in perl-ish example

L. A. Walsh

Tim Chase wrote:
>> BTW, is there a way (or a reason why the ability was disabled)
>> to always use "**very magic**" for interactive regular
>> expressions?
>
> I have similar mental-taxation going the other way...from Vim to
> (in my case) Python or sed syntax.  As with many, many, many
> other things, vim has an option for that.  You can just
>
>    set magic
---
        Actually _*unlike*_ "many, many, many other thing", vim doesn't
seem to have a set *verymagic* ....which is the setting I
wanted active for interactive expressions, by default.  By the time
I realize I need 'very' magic, I'm already half way over or finished
typing the expression, and I'm to used to using "/" to search to
remember to type an extra /v each time.  It would be nice if the
"verymagic" option was configurable like "many, many, many
other things" as you so aptly put it. :-)  (That's why I asked why
'verymagic' was disabled -- seems like every other option is
provided, but not that one...)


Thanks for the pointers on the /L/luUe...etc...will read up on them...
***Much*** of using vim is just learning how & where to search for
something in the help.

Thanks  again!
-l


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to speak vimish for a perl-ish example

L. A. Walsh
In reply to this post by Tim Chase

Tim Chase wrote:
> sounds like you're reaching for the \L replacement modifier:
>
>    :'<,'>s/{[A-Z_]\+}/\L/g
---
        Well that particular expression makes the whole
"variable" go away...anything between curly brackets and the brackets.
But that and your help pointer:
     > which you can read about at
     >    :help s/\L
helped me come up with:

        :'<,'>s/{\([A-Z_]\+\)}/{\L\1\E}/g
Which seems to be a proper string of Vimish to do what I meant. :-)

> I have similar mental-taxation going the other way...from Vim to
> (in my case) Python or sed syntax.
---
sed is pretty similar to perl, though not python...but I also
usually use the "Extended" standard POSIX in sed.  "It'd be nice"
if there was better language integration with python (and 'maybers'
ruby and java too), but of the bunch -- perl was designed for
text-string processing and seems best suited for use as an editing-extension
language.  If it were semantically & syntactically possible, I'd have the
regex-engine configurable so you could drop
in compatible regex engine from any of them.

The current form of perl-vim interfacing requires not only
learning all the vim syntax, but also, all the special vim-edit calls within any
perl code to actually do something with the text, whereas
the syntax used for stating my problem doesn't require the called language to
have special knowledge of the editor -- the editor would just feed the text in
whatever 'range', to an already loaded "DLL" and read out the result, replacing
whatever text was selected for input into the language
"filter".

So if I had a perlflag set for my interactive regexp's, it could feed
the text in a stream to the filter, with, *maybe* (as I am stressing,
don't know how practical it is within the current vim framework) the
filter returning a "byte-offset" (or a pair: start,len) of where
it found the pattern, then Vim would reposition the screen accordingly.
Wrap-around searching could be handled by simply wrapping the text stream
fed to the filter.  When entire file has been fed to filter, send filter
and "EOF", and search-filter returns a failure-to-find error.

But screen editors can be deceptively simple from a GUI standpoint.  It may not
be as crucial now, but I know earlier in the microcomputer industry, it was
expected that an editor should handle large files while still running in
available memory -- essentially managing it's own dynamic paging of parts of the
edited file to one or more temp files.  Now, one might argue that the OS can
handle swapping and limit text file editing to whatever amount of virtual memory
your process can use.  Even with no paging file, one it's not hard to edit have
enough memory to edit far larger files than are practical for an interactive
editor (getting things on and off of
disk being a prime bottleneck when talking Gigabyte sized files).

Sigh.  Thanks again for the right pointers...  If we could just build human
intelligence into the help system maybe that would solve alot of these 'trivial'
issues...:-)

linda

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to speak vimish for a perl-ish example

Tim Chase

> Tim Chase wrote:
>> sounds like you're reaching for the \L replacement modifier:
>>
>>    :'<,'>s/{[A-Z_]\+}/\L/g
> ---
> Well that particular expression makes the whole
> "variable" go away...anything between curly brackets and the brackets.
> But that and your help pointer:
>      > which you can read about at
>      >    :help s/\L
> helped me come up with:
>
> :'<,'>s/{\([A-Z_]\+\)}/{\L\1\E}/g

Whoops...I must have missed a "&"...the original should have read

   :'<,'>s/{[A-Z_]\+}/\L&/g

because in your example, an uppercase "{" or "}" is just the same
character, so you can just lower-case the entire input/match.

-tim



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to substitute (lower) case in a pattern like in perl-ish example

jjlu...@yahoo.com
In reply to this post by L. A. Walsh
Tim, thank you for pointing out the help ( :help s/\L ) for the case replacement and more specifically the how to end the case replacement using the \e or \E... it probably should have been intuitive on how to find the correct ending using the help, but your reply pointed me in the right direction..   thanks again

On Thursday, April 24, 2008 at 12:37:05 PM UTC-5 [hidden email] wrote:
Is there an 'easy' (:-)) (I could write a program, but not exactly my
idea of an easy editing task)...still might end up being the fastest
way to do this...) way to change case of a 'bunch' of identifiers
delimited by curly brackets in a 'block' of text?

I.e. I have a module that currently uses variables of the form:

pkg{VARA}=pkg2{VAR_B}+$offsetp->{VC};

The part that I want to change is the 'upper' case tag in curly
brackets to lower case. So what I'd like is something like a
combination substitute and "tr" (character translate), so I can
do (spaces added for clarity, and using a perl-ish syntax, cuz if I
knew the vim syntax, I'd just use it :-) ):

: '<,'> s / { ([A-Z_]\+) } / { (? { $1=~tr/[A-Z]/[a-z]/ } ) } /xg <- vimish
:<range>s / L (capture ) L / L (? { evaluate-able expr. } ) L <- legend

where - the parens in the first part captures the sub expression;
- above the 'L' in the legend are literal curly braces, delimiting
the captured expression in the search-string, and added back as
literals to the replacement string.
- and everything in (?{replacement-expr}) with the evaluated contents
of a "tr" (translate) substitution operator that operates on
the left-variable of the "=~" expression -- so "tr/[A-Z]/[a-z]/"
would replace every char in the captured-sub-expression (represented
by "$1") that matches in the 1st range "[A-Z]" to it's corresponding
character in the target range "[a-z]" and return the result as the
contents of the replacement sub-expression "(?{$1=~tr/[A-Z]/[a-z]/})",
which would itself be between the two literal curly brackets in the
replacement string.

==== that's "it" ====

Equivalently, if I could run the same command in perl (I have the
perl5.8.dll, but it doesn't seem to be integrated in a way to
do this:
: '<,'> <-- starting with a vi "Visual" range, and appending a perl
command to operate on the range like:
: '<,'> perl 's/ {([A-Z_]+)} / { (?{$1=~tr/A-Z/[a-z]/}) } /xg' <- vimish
' <-- perl expression between single quotes --> ' <- legend

So what would be the equivalent line of vim-code to do what
I want above? I happen to know the perlish way to do it (or at
least I think I do -- haven't tried the above to be honest, but it
was more intended to communicate what I want than being a working
perl example :-), mostly because I know the POSIX (*nix) search and
command set (I believe 'tr' the utility is also part of the POSIX
system command set, and perl was originally designed as an amalgamation
of of several commands that have ended up being part of the POSIX
command set.

Only problem with perl is it didn't include the 'vi[m]' editor :-).
I'm sure 'vim' would somehow be a great feature addition
in perl 5.12. (Perl6, being a fairly different language from its
predecessors, doesn't appear to fulfill the mission of being the
next version or evolution of perl5.8 or 5.10. Apparently the author
admits he only chose the name "perl6" for the perl-name recognition,
not because it was intended to be a compatible upgrade, operators and
variable syntax have even changed.)

BTW, is there a way (or a reason why the ability was disabled) to
always use "very magic" for interactive regular expressions? Sure
would help me in remembering more of the expression syntax. I never
know if I need an extra "\" to make something have it be treated
as its 'normal' Regex character function (magic) vs. being treated
as a literal without trying to look it up in a chart -- and even
then it's confusing with things like square-brackets not being
symmetric in their backslash requirements. I understand the logic
of why it is the way it is, but that doesn't mean it's not a
different logic and mind-set from where 'symmetry' would take
precedence over necessity.


Thanks for any help & pointers...
-Linda


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/f7d9bf06-4850-475d-80e5-c917cdfdaae9n%40googlegroups.com.