#1
  1. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2008
    Posts
    1,928
    Rep Power
    378

    Simple regular expression


    In my string '$string', I want to replace every instance of ". [A-Z]",

    i.e., a full stop, followed by a space, followed by a capital later

    with ".<br><br>[A-Z]"

    but I'm such a numpty when it comes to regular expressions.

    Help!

    Edit: Oops, I wonder if some good-natured soul would be so kind as to move my thread to the regex forum 'which we now have'!!!

    Thanks
    Last edited by cafelatte; January 24th, 2012 at 06:31 AM.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    Done. Thanks for noticing.

    Search on
    Code:
    /. ([A-Z])/
    Use $1 to insert the letter back into the replacement string, since that's the only variable part of the whole thing.
  4. #3
  5. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Hi cafelatte,

    I'd suggest one tiny change from Requinix's suggestion because you want to match a literal period, not a "dot" (any character).

    I have tested this php.

    Input:
    Period. Again.One.Word. Hi.5.33

    Code:
    PHP Code:
    <?php
    $regex
    =',\. ([A-Z]),';
    $string='Period. Again.One.Word. Hi.5.33';
    echo 
    htmlentities(preg_replace($regex,'.<br /><br />\1',$string));
    ?>
    Output:
    Period.<br /><br />Again.One.Word.<br /><br />Hi.5.33

    The htmlentities is just for display, what you want is the preg_replace.

    Let us know if one of those works for you.
    :-)
    Last edited by ragax; January 27th, 2012 at 08:32 PM. Reason: Inserted space char in the regex

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo