#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Location
    UK
    Posts
    11
    Rep Power
    0

    Struggling with my first regular expression!


    Hi

    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.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Location
    UK
    Posts
    11
    Rep Power
    0
    I've since come up with this...

    ^D([0-9]+)([A|E|V])([0-9]+)^

    which seems to give me what I am looking for!

    Any advice welcome
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,112
    Rep Power
    9398
    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.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,112
    Rep Power
    9398
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Location
    UK
    Posts
    11
    Rep Power
    0

    Thumbs up


    thanks for your help. I slowly getting to understand it!

IMN logo majestic logo threadwatch logo seochat tools logo