raven93
raven93

Reputation: 91

PowerGREP - regular expression

I have log of Apache and each line of file looks like:

script.php?variable1=value1&variable2=value2&variable3=value3&.........................

I need to take out this part of string:

variable1=value1&variable2=value2

and ignore the rest of line. How I can do this in PowerGREP?

I tried:

variable1=(.*)&variable2=(.*)&

But I get rest of line after value2.

Please help me, sorry for my english.

Upvotes: 2

Views: 373

Answers (2)

Armali
Armali

Reputation: 19375

Contrary to what Ed Cottrell wrote about his second example, the first one works better (i. e. correctly); this is because if the subexpression for value2 is made non-greedy, it matches as few characters as possible, i. e. not any.


If you wouldn't mind having the & after value2 included in the match, you could as well hone your try by making the subexpression for value2 non-greedy, so that it only extends to the next &:

variable1=(.*)&variable2=(.*?)&

Upvotes: 1

elixenide
elixenide

Reputation: 44831

Replace . with [^&] and drop the final &, like this:

variable1=(.*)&variable2=([^&]*)

. will match anything it can (any character except for the newline character, basically). [^&], on the other hand, matches only characters that are not &.

For even better results and faster performance, you can also replace the first . in the same way and add ? (the non-greedy qualifier), like so:

variable1=([^&]*?)&variable2=([^&]*?)

Here's a working demo.

Upvotes: 0

Related Questions