2

really struggling with this...hopefully someone can put me on the right path to a solution.

My input string is structured like this:

66-2141-A-AC107-7

I'm interested in extracting the string 'AC107' using a single regular expression. I know how to do this with other PHP string functions, but I have to do this with a regular expression.

What I need is to extract all data between the third and fourth hyphens. The structure of each section is not fixed (i.e, 66 may be 8798709 and 2141 may be 38). The presence of the number of hyphens is guaranteed (i.e., there will always be a total of four (4) hyphens).

Any help/guidance is greatly appreciated!

2 Answers 2

3

This will do what you need:

(?:[^-]*-){3}([^-]+)

Regular expression visualization

Debuggex Demo

Explanation:

  • (?:[^-]*-) Look for zero or more non-hyphen characters followed by a hyphen
  • {3} Look for three of the blocks just described
  • ([^-]+) Capture all the consecutive non-hyphen characters from that point forward (will automatically cut off before the next hyphen)

You can use it in PHP like this:

$str = '66-2141-A-AC107-7';
preg_match('/^(?:[^-]*-){3}([^-]+)/', $str, $matches);
echo $matches[1];  // prints AC107
Sign up to request clarification or add additional context in comments.

3 Comments

Wow! Ed Cottrell...above and beyond! Clear, concise and explained. Thank you very much
To the downvoter (who I assume is the same person who just commented and deleted the comment): Technically, you are right, the ^ isn't necessary for the example given. I edited my answer for clarity.
Not sure why anyone would downvote this correct and efficient answer. +1
0

This should look for anything followed by a hyphen 3 times and then in group 2 (the second set of parenthesis) it will have your value, followed by another hyphen and anything else.

/^(.*-){3}(.*)-(.*)/

You can access it by using $2. In php, it would be like this:

$string = '66-2141-A-AC107-7';
preg_match('/^(.*-){3}(.*)-(.*)/', $string, $matches);
$special_id = $matches[2];
print $special_id;

4 Comments

Very poor (and unnecessary) use of dot star. Note that: /^(.*-){3}(.*)-(.*)/ fails when there are more than 4 hyphens in the string. See: @Ed Cottrell's answer for a much better way to craft this regex. If you plan on offering up more answers to regex related questions, you would do well to go read Mastering Regular Expressions (3rd Edition).
Thank you for the link, ridgerunner. I will see if I can acquire that book. I love working with Regular Expressions and I am always looking for ways to improve my skills in this area. -Rick
I guarantee You will not regret it. MRE3 is hands down, the most useful book I've ever read. Note that I programmed professionally for more than 30 years (low level assembly, C, FORTRAN, etc) before I started using regex. Once I discovered them, (and read MRE3), there was no turning back (and I kick myself 1000 times for not learning them sooner). Now I can't live without them! Regular expressions are now the most powerful (and, yes, FUN) tool in my (text-processing/editing) toolbox.
Yeah man, for real. Everything I have picked up regarding REGEX has been little bits here and there. I didn't have anyone to teach me this stuff, so it's taken me a long time to finally be comfortable with using them. My employer has a Safari Online account, so I am in the process of obtaining that book as we speak. Thanks again ... I look forward to being a REGEX guru one day! -Rick

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.