[ale] Regular Expression.

Björn Gustafsson bg-ale at bjorng.net
Tue Sep 4 13:26:14 EDT 2007


Are you doing a Perl regex, because the thing you need varies somewhat
depending on the particular regex library.  Generally though you'll
probably want a non-greedy specifier, or you'll want to reduce the set
of viable characters to exclude e.g. [].

As I recall the "." class in Perl already excludes a newline so if you
meant to include that in the set you already have a problem.  If by
"What follows" you mean what might be after the existing regex match,
that's not really an issue.

Based on your existing regex, I'd do something like one of the following:

's/$_GET\[\('.*?'\)\]/get_get(\1)/g;'

's/$_GET\[\('[^\[\]\n]*'\)\]/get_get(\1)/g;'

The exact way to escape the [] inside the inner
character-class-specifier depends heavily on your particular
language/library, but hopefully you get the idea.

http://www.bayview.com/blog/2003/02/12/non-greedy-regular-expressions/

On 9/4/07, Michael B. Trausch <mike at trausch.us> wrote:
> For the life of me, I cannot figure out a regex that will do what I want
> it to do.  I want to change every instance of the string:
>
>         $_GET['varname']
>
> to:
>
>         get_get('varname')
>
> Using something like sed to automagically do the work for me.  I tried
> to do this, however, using the following regex:
>
>         's/$_GET\[\('.*'\)\]/get_get(\1)/g;'
>
> ... which only works if there is one $_GET[''] on the line.  If there
> are more than one, though, it does something like this:
>
> if ((isset(get_get('id'])) && ($_GET['id') != ''))
>
> Is there a way to make the \[\('.*'\)\] part of the regex less greedy so
> that it will match EXACTLY one instance?  What follows may be any
> character, including a newline.
>
>         -- Mike

-- 
Bj?rn Gustafsson



More information about the Ale mailing list