[RESOLVED] preg_replace insert hyphen -- ABC123 -> ABC-123, A1 -> A-1, etc.
Results 1 to 8 of 8

Thread: [RESOLVED] preg_replace insert hyphen -- ABC123 -> ABC-123, A1 -> A-1, etc.

  1. #1
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,676

    resolved [RESOLVED] preg_replace insert hyphen -- ABC123 -> ABC-123, A1 -> A-1, etc.

    I'm up against a brick wall on this one ... coming into work with the top down seems to have addled my already troubled mind

    My "in string" is of this format:
    PHP Code:
    $std_regex1="%[\w]{1,3}\d{1,5}%";   //A1, ABC12345 
    I want to separate the alpha character(s) from the numerics with a hyphen - the desired "out string" is "A-1" in the first example case, and "ABC-12345" in the second.

    So far, I've drawn a blank on this with preg_replace() and was actually considering looping through the entire string to prepend a hyphen to the first instance of a numeric character, but I'm already way too expensive to scale this.

    A good 1/2 hour of searching here and the big G isn't helping, either. Maybe more breakfast/coffee?

    Any pointers are appreciated ... I'm sure I'm just being thick as usual.
    Last edited by dalecosp; 10-24-2012 at 11:26 AM. Reason: removed nasty whitespace from the regex and fix speling ;)
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  2. #2
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,765
    Give this a try:

    PHP Code:
    $array = array('A1','ABC12345');
    foreach( 
    $array as $k => $v ) {
       
    $array[k] = preg_replace('/(\w{1,3})(\d{1,5})/','\1-\2',$v);
    }
    var_dump($array); 
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  3. #3
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,676
    Thanks! It's close, but different test cases fail. Did I not give adequate test cases? The regex will also match things like ab2345, yz33, etc.

    Code:
    [574] Wed 24.Oct.2012 11:28:43 [kevin@rootbsd][~] cat foo; php foo; cat bar; php bar
    <?php
    $array = array('AB2345','yz33');
    foreach( $array as $k => $v ) {
       $array[$k] = preg_replace('/(\w{1,3})(\d{1,5})/','\1-\2',$v);
    }
    
    var_dump($array);
    array(2) {
      [0]=>
      string(7) "AB2-345"
      [1]=>
      string(5) "yz3-3"
    }
    
    <?php
    $array = array('a1','abc123');
    foreach( $array as $k => $v ) {
       $array[$k] = preg_replace('/(\w{1,3})(\d{1,5})/','\1-\2',$v);
    }
    var_dump($array);
    array(2) {
      [0]=>
      string(3) "a-1"
      [1]=>
      string(7) "abc-123"
    }
    If it could get the hyphen one character sooner in the other cases it looks golden ... though I need to throw a few thousand test cases at it to be certain.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  4. #4
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,765
    Well, then do this instead:
    PHP Code:
    <?php

    $array 
    = array('A1','ABC12345','AB2345','yz33');
    foreach( 
    $array as $k => $v ) {
       
    $array[$k] = preg_replace('/([a-z]{1,3})(\d{1,5})/i','\1-\2',$v);
    }
    var_dump($array);
    Output:
    Code:
    >php test2.php
    array(4) {
      [0]=>
      string(3) "A-1"
      [1]=>
      string(9) "ABC-12345"
      [2]=>
      string(7) "AB-2345"
      [3]=>
      string(5) "yz-33"
    }
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,676
    Blargh, yes, indeed. Just did it for myself. Thanks. I need a lunch break!
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,676
    Shouldn't have been using "\w" in the first place on this one, I think

    It was imported from another function where it needed to be less restrictive.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  7. #7
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,637
    I'd recommend something I find a lot simpler in this case. Create a pattern consisting of a looh behind assertion for a-zA-Z and a look ahead assertion for 0-9. That is, you'll be matching the spot where a letter is followed by a didigt, and replace that spot with a -. That there isn't an actual character in that particular spot doesn't matter, it works anyway.

    Code:
    $arr = array(
    	'abc123',
    	'ZY99',
    	'ASDDQWE4564',
    	'ASD',
    	'123',
    );
    
    $re = '#(?<=[a-zA-Z])(?=[0-9])#';
    
    foreach ($arr as $s)
    {
    	$out = preg_replace($re, '-', $s);
    	echo $out.'<br>';
    }

  8. #8
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,765
    Stop being fancy johanafm. :P
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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