Splunk Search

How to apply regex to a field?

msalghamdi
Path Finder

Hello Splunkers, 

 

i want to to extract a 10-digit path from a url but unfortunately i always get this error:

Error in 'rex' command: The regex '.*\/(([0-9a-z]{10}))' does not extract anything. It should specify at least one named group. Format: (?<name>...).

 

how ever, i want to extract the path from this URL  https://example.com/8a2a6063b3

this is the search i used

 

index=FP_proxy | rex field=url "http[s]?:\/\/[a-zA-Z0-9-]{1,}\..*\/(([0-9a-z]{10})?<url__path>)"

your help is much needed and appreciated to fix this issue

 

 

 

Labels (4)
0 Karma
1 Solution

javiergn
Super Champion

Hi @msalghamdi , the capturing group in your regex has the wrong format. Try moving the label to the beginning. Like this:

index=FP_proxy | rex field=url "http[s]?:\/\/[a-zA-Z0-9-]{1,}\..*\/(?<url__path>[0-9a-z]{10})"

Which is going to extract 8a2a6063b3 into the url__path field.

Hope that helps.

Thanks,

J

View solution in original post

javiergn
Super Champion

Hi @msalghamdi , the capturing group in your regex has the wrong format. Try moving the label to the beginning. Like this:

index=FP_proxy | rex field=url "http[s]?:\/\/[a-zA-Z0-9-]{1,}\..*\/(?<url__path>[0-9a-z]{10})"

Which is going to extract 8a2a6063b3 into the url__path field.

Hope that helps.

Thanks,

J

msalghamdi
Path Finder

thanks javiergn

one more question please, i want to apply a condition on the extracted field that is must exist, here's my search:

index=proxy | rex field=url "http[s]?:\/\/[\w]{1,}\.[\w]{1,}\/(?<ppp>[0-9a-z]{10})$"
| where ppp=*
| table _time src dest_ip dest user ppp url status

but i get this error:

Error in 'where' command: The expression is malformed. An unexpected character  is reached at '* '. 

what can i do to fix this ?

 

thanks

0 Karma

javiergn
Super Champion

Hi, use search instead of where and problem solved.

Or you could also do | where isNotNull(ppp)

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...