Splunk Search

rex field can not work

maryang
New Member

Below is the my query:
index=app splunk_server_group=CWE sourcetype=ELMTP99 host="CHE-elmAPP0" source="C:\TPles\ELMgFile.log" sourcetype="elm99"
msgId=abc-* OR msgId=sdv-* OR msgId=wer-* OR msgId=qwe-*
| rex field=msgId "(?.)-"
And it throws error like below:
Error in 'rex' command: Encountered the following error while compiling the regex '(?.
)-': Regex: unrecognized character after (? or (?-

How do I fix that issue?

Tags (1)
0 Karma
1 Solution

hunters_splunk
Splunk Employee
Splunk Employee

Hi maryang,

I think you should supply in the name of the field to which you want to assign the reg-extracted values. The new field values should be included in brackets (). For example, if you want to assign reg-extracted values to a field named msgprefix, use the following search:

index=app splunk_server_group=CWE sourcetype=ELMTP99 host="CHE-elmAPP0" source="C:\TPles\ELMgFile.log" sourcetype="elm99" 
msgId=abc- OR msgId=sdv- OR msgId=wer- OR msgId=qwe-
| rex field=msgId "(?<msgprefix>.)-"

Hope it helps. Thanks!
Hunter

View solution in original post

0 Karma

hgrow
Communicator

Hi maryang,

execute the following search in splunk:

| makeresults | eval msgId="abc-" | rex field=msgId "(?<name_your_field>.*)-"

Your regex is not quite right. Your capturing group must be a so called "naming group" Next . just referes to any character except line break but just once. you want any character until a -. By following your approch you will add * (zero or more times) to the ..

A maybe little bit better approach might be to capture everything from the beginning of the line which is not a -:

 | makeresults | eval msgId="abc-" | rex field=msgId "(?<name_your_field>^[^-]+)-"

Sincerely,
hgrow

0 Karma

hunters_splunk
Splunk Employee
Splunk Employee

Hi maryang,

I think you should supply in the name of the field to which you want to assign the reg-extracted values. The new field values should be included in brackets (). For example, if you want to assign reg-extracted values to a field named msgprefix, use the following search:

index=app splunk_server_group=CWE sourcetype=ELMTP99 host="CHE-elmAPP0" source="C:\TPles\ELMgFile.log" sourcetype="elm99" 
msgId=abc- OR msgId=sdv- OR msgId=wer- OR msgId=qwe-
| rex field=msgId "(?<msgprefix>.)-"

Hope it helps. Thanks!
Hunter

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...

Network to App: Observability Unlocked [May & June Series]

In today’s digital landscape, your environment is no longer confined to the data center. It spans complex ...

SPL2 Deep Dives, AppDynamics Integrations, SAML Made Simple and Much More on Splunk ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...