Regex Help

Aanvik
Aanvik used Ask the Experts™
on
I need a simple regex for the following string

Brad-233

so here it's firstname-last 3 chars of id.

Can someone pls help here...?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
[A-Za-z]+-\d{3}
If you need this in PHP

<?php

$data = "Brad-232";

$match = array();

preg_match( '#^([\S]+)-([\S]*)([\S]{3})$#i', $data, $match );

print_r( $match );

Your data is in $match[1] and $match[3]. I have done it this way because you said the last 3 characters of the ID so Brad-9999123 would return Brad in $match[1] and 123 in $match[3]
As an alternative, you could just split this using explode

$data = "Brad-123";
$arr = explode( '-', $data );
echo $arr[0];
echo '<br/>';
echo $arr[1];

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial