Splunk Search

Regex error

Puzzle2011
Engager

I am trying to validate that the user has entered their phone number in this format (555) 555-5555. I keep getting this error: Unmatched ) in regex; marked by <-- HERE in m/[([0-9]) <-- HERE ]+[[0-9]-]+[[0-9]+/ at validate.cgi line 46. My code is below. Any help?

sub validate_input {
my $valid = "Y";
my $errormsg;
if ($phone !~ m/[([0-9])]+[[0-9]-]+[[0-9]+/]) {
$valid = "N";
my $errormsg = "enter a valid phone number in format desired.";
}
return $valid, $errormsg;
}

Tags (2)
0 Karma
1 Solution

tpflicke
Path Finder

The characters ( and ) in regex are used for grouping so you need to escape them if you want to match literally.
You can also use \d for digit, I find it more readable than [0-9].
Try

m/\(\d\d\d\)\s*\d\d\d-\d\d\d\d/ 
m/\(\d{3}\)\s*\d{3}-\d{4}/
m/\(\d+\)\s*\d+-\d+/

depending on whether you want to exactly match the number of digits and preference of style.

View solution in original post

tpflicke
Path Finder

The characters ( and ) in regex are used for grouping so you need to escape them if you want to match literally.
You can also use \d for digit, I find it more readable than [0-9].
Try

m/\(\d\d\d\)\s*\d\d\d-\d\d\d\d/ 
m/\(\d{3}\)\s*\d{3}-\d{4}/
m/\(\d+\)\s*\d+-\d+/

depending on whether you want to exactly match the number of digits and preference of style.

Get Updates on the Splunk Community!

Devesh Logendran, Splunk, and the Singapore Cyber Conquest

At this year’s Splunk University, I had the privilege of chatting with Devesh Logendran, one of the winners in ...

There's No Place Like Chrome and the Splunk Platform

WATCH NOW!Malware. Risky Extensions. Data Exfiltration. End-users are increasingly reliant on browsers to ...

Customer Experience | Join the Customer Advisory Board!

Are you ready to take your Splunk journey to the next level? &#x1f680; We invite you to join our elite squad ...