April 17th, 2012, 10:09 AM
Struggling with my first regular expression!
I've been trying to figure this out half the afternoon!! It's my first attempt at using preg_match and regular expressions and I am really struggling. I could do it using other methods based on a number of if's and strpos, etc but I know this is not the best way.
I have a short string, for example D34V78. I would like to split this into four elements, with the result an array of D, 34, V and 78
The D (first character) is always D.
It is then always followed by a number of unknown length
Then a V, A or E
then another number of unknown length
Further examples.... D1V3 or D98E62
Can some kind person help me by suggesting the regular expression -- my little brain is in melt down with it.
April 17th, 2012, 10:52 AM
I've since come up with this...
which seems to give me what I am looking for!
Any advice welcome
April 17th, 2012, 11:39 AM
You've got it right. There isn't really anything that can be improved with it.
But if I can I have a suggestion: use a different delimiter than a ^. It could be confusing: carets are beginning-of-string anchors so it's very common to see them and especially at the start of an expression.
For completeness I should mention that your expression will match the string if it's to be found anywhere in the search text. If you need to make sure the text is exactly in that form and nothing else (eg, you're validating it) then you need ^ and $ anchors. Otherwise if you're confident the text is correct - and I believe this is the case - then you're fine.
April 17th, 2012, 12:31 PM
As pointed out by .josh, I did miss something: the pipes. Yet again I've been up for a long time so that's totally what I'm going to blame. To restate what he said,
Inside a  a pipe doesn't mean anything special. It's just another character. So [A|E|V] means an A, E, V, or a pipe. You don't want that. Either use the pipe without the s like (A|E|V) or vice versa like ([AEV]) (which is arguably better).
Also not mentioned is the (over?) use of capturing groups. This isn't one of those times when it really matters so I didn't say anything. It is more to type though.
April 17th, 2012, 01:16 PM
thanks for your help. I slowly getting to understand it!