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

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0

    Question Replace single caps only with same letter plus dot, or period.


    Hi,

    I can place a space in front of all caps, and I can put a period behind all caps, but I can't put a period only behind those caps that are single, or stand alone. This is necessary when dealing with middle initials, after they have been stripped of punctuation to create virtually identical image and page names. Please help!

    <?php
    $string = file_get_contents('a.txt');
    $string = ltrim(preg_replace('/[\A-Z]/', ' $0. ', $string));
    echo "$string";
    ?>

    This code places a space before and a dot after every capital letter.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,995
    Rep Power
    9397
    So you only want to replace capital letters that are surrounded by things that aren't capital letters? Use (?<!) and (?!) to indicate what should not show up before and after the letter:
    Code:
    /(?<![A-Z])[A-Z](?![A-Z])/
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by requinix
    So you only want to replace capital letters that are surrounded by things that aren't capital letters? Use (?<!) and (?!) to indicate what should not show up before and after the letter:
    Code:
    /(?<![A-Z])[A-Z](?![A-Z])/
    Well, where the names JimKPigseye@LLBean@AnyDName are concerned, my code gives J. im K. P. igseye@ L. L. B. ean@ A. ny D. N. ame . The @ sign is just a way of marking the division between names.

    The code you proposed gives J. imKPigseye@LLBean@ A. nyDName .

    What I need is Jim K. Pigseye@L.L. Bean@Any D. Name

    L. L. Bean would be close enough.

    I know it's a toughy, but it seems such an obvious use would have been accomplished by now, somewhere. I just can't find where. Thanx!
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,995
    Rep Power
    9397
    It's not difficult - I'm just trying to get a precise explanation of what you want.

    So now you're saying to add periods after every capital letter that is followed by another capital letter?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by requinix
    It's not difficult - I'm just trying to get a precise explanation of what you want.

    So now you're saying to add periods after every capital letter that is followed by another capital letter?
    Yes, a period and a space behind every capital letter that is followed by another capital letter may be the best way of formatting the problem.

    Another way of seeing it, one that would not permit everything to be done in one pass, is to put a space in front of every capital letter first, and then save that result to a file.

    Then, run a second regex, one that puts a period only behind capital letters that stand alone, or any letter that stands alone, for that matter, but the only ones will be capitals.

    I think your way must be the better way of looking at it, since you seem to have education and experience. I have none, at all, where regex is concerned; though I know it is an essential field for programmers. Really, I am a web-developer, not a programmer.

    Thanks a million in advance for any help you can offer me.
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,995
    Rep Power
    9397
    The simplest way to approach a regex problem is generally the best
    Code:
    /[A-Z](?=[A-Z])/ => '$0. '
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by requinix
    The simplest way to approach a regex problem is generally the best
    Code:
    /[A-Z](?=[A-Z])/ => '$0. '
    Hi,

    I ran the code you wrote as seen below and got the message that follows it. Is it possible that the flavor that you are working in is incompatible with Php? I tried with and without single quotes. Nothing else is changed.

    <?php
    $string = file_get_contents('a.txt');
    $string = ltrim(preg_replace('/[A-Z](?=[A-Z])/' => '$0. ', $string));
    echo "$string";
    ?>

    Parse error: syntax error, unexpected T_DOUBLE_ARROW in /hermes/path-to-my-file/0-/spaces/e.php on line 5

    Thank you!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by requinix
    The simplest way to approach a regex problem is generally the best
    Code:
    /[A-Z](?=[A-Z])/ => '$0. '
    Hi, I got it to work like this:

    <?php
    $string = file_get_contents('a.txt');
    $string = ltrim(preg_replace('/[A-Z](?=[A-Z])/' ,'$0. ', $string));
    echo "$string";
    ?>


    However, I still have to make the replacements between the caps that are not followed by other caps.

    Thanks a million; it still might work out.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Got it!
    Used your code first, and then this code on a second file made from the output of your code:
    <?php
    $string = file_get_contents('b.txt');
    $string = ltrim(preg_replace('/[A-Z]/', ' $0', $string));
    echo "$string";
    ?>
    Works like a charm.

    Thanks a million! Case closed.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by jagtig
    Got it!
    Used your code first, and then this code on a second file made from the output of your code:
    <?php
    $string = file_get_contents('b.txt');
    $string = ltrim(preg_replace('/[A-Z]/', ' $0', $string));
    echo "$string";
    ?>
    Works like a charm.

    Thanks a million! Case closed.
    Here's the entire solution for the thousands who visit this discussion in years to come:

    First run the script below on a file called "a." It will produce periods behind caps followed by spaces where caps follow caps, only. Caps followed by lower-case letters will not have periods placed behind them.

    <?php
    $string = file_get_contents('a.txt');
    $string = ltrim(preg_replace('/[A-Z](?=[A-Z])/','$0. ', $string));
    echo "$string";
    ?>

    Then, copy the output to a file called "b," and run the code below on that. It will place spaces in front of caps. That should do it. If not, you may have permission, php or other server-related issues.

    <?php
    $string = file_get_contents('b.txt');
    $string = ltrim(preg_replace('/[A-Z]/',' $0', $string));
    echo "$string";
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo