[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] regular expressions



On Tue, Mar 14, 2000 at 02:00:47PM +1030, jakal wrote:
> I'm having difficulty with regular expressions, and I lent my only perl
> book to a friend, so while I'm struggling away, I wondered if any regexp
> guru's might help.
> 
> I have a string that looks something like this:
> 
> question1=What+is+your+name%3F&question2=How+old+are+you%3F&question3=Gender%3F&
> question4=&question5=&question6=&question7=&
> 
> I want to find the first occurance of a = followed by a & (=&), backtrack
> to the previous &, and remove everything after and including that &.

I think your difficulty comes from your specification of the problem.
Regular expression backtracking does not work in this manner; backtracking
is for when a part of the match fails, not when it succeeds.

What you want to do is find an &, some characters, and an =&, and then
replace it with an &.

Try this:

s/&[^&]*=&/&/g;


However, if the sequences occurs twice in a row (&foo=&bar=&), it will only
match the first, because it uses both &s.  So try a positive lookahead
assertion instead:

s/&[^&]*=(?=&)//g;

Now the second ampersand isn't consumed by the match.


HTH!
Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org