SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex - matching string termination?

    What would one do to match the termination of a string?

    For instance
    PHP Code:
    $string "A sentence. More words. Dot.End.";
    $string preg_replace("#(\.[\sE])#""[INS]$1"$string);
    echo 
    $string
    Results in:
    Code:
    A sentence[INS]. More words[INS]. Dot[INS].End.
    How would one match the end of the string as well as "\s" and "E", so that it becomes:
    Code:
    A sentence[INS]. More words[INS]. Dot[INS].End[INS].

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string "A sentence. More words. Dot.End.";

    $string preg_replace("#((\.[\sE])|\.$)#""[INS]$1"$string);

    echo 
    $string

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Whilst decowski's regex would work just fine, here's a regex which more literally describes what you're after: match a dot, only when followed by either whitespace or the capital E character or the end of the string and replace that dot with "[INS].". It makes uses of a lookahead assertion to catch only the dots that you want.

    PHP Code:
    $string preg_replace("#\.(?=\s|E|$)#""[INS]."$string); 
    Off Topic:


    If you are unfamiliar with the syntax or concept of lookahead assertions (in this case, to match dots followed by specific characters or points in the string) then a helpful introduction can be found in Lookahead and Lookbehind Zero-Width Assertions.

    A more basic expression with which you might be more familiar would be #\.(\s|E|$)# with the associated replacement string [INS]$0.
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    PHP Code:
    $string preg_replace("#\.(?=\s|E|$)#""[INS]."$string); 
    Alternatively (yes, it's a minor alternative):

    PHP Code:
    $string preg_replace("#(?<=(?:\s|E\$)\.)#""[INS]"$string); 
    At least, it seems to yield the same results. Splitting hairs in the grand scheme of things admittedly.

  5. #5
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot guys

    I had originally tried this pattern
    Code:
    #(\.[\sE$])#
    But that didn't work so I had thought $ wasn't the right thing to use - but I guess it's perhaps the brackets taking the $ as a literal?

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nrg_alpha View Post
    Alternatively (yes, it's a minor alternative):
    PHP Code:
    $string preg_replace("#(?<=(?:\s|E\$)\.)#""[INS]"$string); 
    At least, it seems to yield the same results. Splitting hairs in the grand scheme of things admittedly.
    I'm not sure how you tested this but it really should not yield the same results. It is looking for a point in the string with either of the following before it: a)a whitespace character followed by a dot, b)capital letter E followed by the end of the string followed by a dot (not possible!).

    Quote Originally Posted by Kadence View Post
    I guess it's perhaps the brackets taking the $ as a literal?
    Absolutely; the $ character has no special meaning within a character class [$].
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I retested and it doesn't work.. I'm not sure what I tested previously.. perhaps a previous result from a previous example that I mistook as the current one or something.. shakes head..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •