cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

This product reached the end of support date on March 31, 2021.

Regex for capturing second to last query

sxsaxena
Participant

I’m trying to capture search terms
from URI query value. Unfortunately, it seems that they pass the old URI as
part of the arguments for a search. So the first time I search it looks nice
and clean and easy like this (line breaks at each new query piece, only query portion of URI shown)

Fresh search MikeBrew7

p_p_id=3

&p_p_lifecycle=0

&p_p_state=maximized

&p_p_mode=view

&_3_struts_action=/search/search

&_3_redirect=/acp/auto

&_3_keywords=MikeBrew7

&_3_groupId=10184

The second time, they’ll pass that
whole string as part of the new search, though the & and other special
characters get replaced with their corresponding % codes. I had the person
search for “MikeBrew1”, then MikeBrew2, so on so forth till MikeBrew6. The previous search gets pushed to a previous 'keyword' entry, and the search before that gets pushed to the 'redirect' portion. The
result looks like this:

Old search after MikeBrew1-6

p_p_id=3

&p_p_lifecycle=0

&p_p_state=maximized

&p_p_mode=view

&_3_struts_action=/search/search

&_3_redirect=/acp/home?p_p_id=3

&p_p_lifecycle=0

&p_p_state=maximized

&p_p_mode=view

&_3_struts_action=%2Fsearch%2Fsearch

&_3_redirect=%2Facp%2Fhome%3Fp_p_id%3D3%26p_p_lifecycle%3D0%26p_p_state%3Dmaximized%26p_p_mode%3Dview%26_3_struts_action%3D%252Fsearch%252Fsearch%26_3_keywords%3DMikeBrew4%26_3_groupId%3D10184

&_3_keywords=MikeBrew5

&_3_groupId=10184

&_3_keywords=MikeBrew6

&_3_groupId=10184

So now I’m trying to figure out
how to only capture the very last instance of &_3_keywords=(search
stuff)&_3_groupID=\d+

In the business transaction, I
have a Filter on web request URI for \/search\/search so now we only pick up
the actual searches.

For the splitting value, im not sure how to make the regex work. It will
find the first result of “keywords” and return EVERYTHING after, so im instead
trying to search starting at the end.

Using positive lookbehind to capture the value wont work because the thing i'm trying to capture is potentially of variable length and there are too many repeating tokens in the query.

7 REPLIES 7

Hi Shrimant,

Can you post an example of a full URI without the line breaks? It'll help me test this on my end.

Thanks,

Dan

sxsaxena
Participant

/acp/home/search/search/p_p_id=3&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&_3_struts_action=/search/search&_3_redirect=/acp/home?p_p_id=3&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&_3_struts_action=%2Fsearch%2Fsearch&_3_redirect=%2Facp%2Fhome%3Fp_p_id%3D3%26p_p_lifecycle%3D0%26p_p_state%3Dmaximized%26p_p_mode%3Dview%26_3_struts_action%3D%252Fsearch%252Fsearch%26_3_keywords%3DMikeBrew4%26_3_groupId%3D10184&_3_keywords=MikeBrew5&_3_groupId=10184&_3_keywords=MikeBrew6&_3_groupId=10184

_JOJO
Dynatrace Helper
Dynatrace Helper

Hello Shrimant,

I used a negative look ahead.

  • &_3_keywords=(\w+)(?!.*&_3_keywords)

This will capture the last search term using the example you gave which would be MikeBrew6

It was also able to match the first example you gave.

Thanks very much,

Joe

this is only capturing the first word of every search. It is not accounting for spaces or other characters. I cant use '.*' in the search term since its greedy, right?

&_3_keywords=(.*)&_3_groupId=\d+(?!.*&_3_keywords)

That is the one that worked.

janusz_dabrowsk
Dynatrace Pro
Dynatrace Pro

.* is greedy, so will it not sufficient to use

.*&_3_keywords=(\w+)&

It requires 62 steps to process while

&_3_keywords=(\w+)(?!.*&_3_keywords)

requires 629 (according to https://regex101.com/ )

sxsaxena
Participant

It Worked!