Splunk Search

How to use rex to remove the domain from the "User name" field and use the username only as a named extraction?

kris99
New Member

How do I use regex within search to remove the domain from the field "User name" and use the username only as named extraction.

domain\username

something like this i think but don't know who to write regex to extract username or extract everything after "\" from field "User name"

| rex field="User name" "" | eval UserName=lower(UserName) | where UserName=lower(UserName) | search UserName="*"

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Try this:

... | rex field="User name" "(?<domain>\S+)\\\\(?<userName>\S+)" | eval userName=lower(userName) | ...

If Splunk doesn't like a field name with a space in it, try this:

... | eval domainUsername="User name" | rex field=domainUsername "(?<domain>\S+)\\\\(?<userName>\S+)" | eval userName=lower(userName) | ...
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

Try this:

... | rex field="User name" "(?<domain>\S+)\\\\(?<userName>\S+)" | eval userName=lower(userName) | ...

If Splunk doesn't like a field name with a space in it, try this:

... | eval domainUsername="User name" | rex field=domainUsername "(?<domain>\S+)\\\\(?<userName>\S+)" | eval userName=lower(userName) | ...
---
If this reply helps you, Karma would be appreciated.

richgalloway
SplunkTrust
SplunkTrust

Please accept the answer.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

yes i did.

just so i learn how to write regex, if it was seperated by : what would i replace it with ?

0 Karma

richgalloway
SplunkTrust
SplunkTrust

In the regex in the answer, the four backslashes are the separator between the domain and username. If the separator becomes ':' then the regex becomes "(?\S+):(?\S+)".

A good way to learn is through experimentation. Try regexr.

---
If this reply helps you, Karma would be appreciated.
0 Karma

richgalloway
SplunkTrust
SplunkTrust

The escape character needs to be escaped. I've updated the answer.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

works like a charm.. thank you !

0 Karma

richgalloway
SplunkTrust
SplunkTrust

What do you get?

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

same error above

0 Karma

richgalloway
SplunkTrust
SplunkTrust

The backslash needs to be escaped.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

only using this.. no luck

rex field=domainUsername "(?<domain>\S+)\\(?<userName>\S+)" 
0 Karma

richgalloway
SplunkTrust
SplunkTrust

The parts between < and > define a Splunk field into which rex will extract matches. They're not placeholders. Change "domain-22" back to "domain" and it should work.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

still getting same error. tried both options above

Error in 'rex' command: Encountered the following error while compiling the regex '(?<domain>\S+)\(?<userName>\S+)': Regex: unmatched parentheses 
0 Karma

kris99
New Member

getting an error as below. domain includes domain-22\username

Error in 'rex' command: Encountered the following error while compiling the regex '(?<domain-22>\S+)\(?<userName>\S+)': Regex: unmatched parentheses 
0 Karma

richgalloway
SplunkTrust
SplunkTrust

Just what? If there's a character between the quotation marks, it's not showing up. Escape the character or use backtics.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

editor is removing backward slash

0 Karma

kris99
New Member
 domain\username
0 Karma

richgalloway
SplunkTrust
SplunkTrust

What separates domain from username? Please share a sample of your data.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

just "\"

"User name"=domain\username

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...