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

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Replacing character in middle of string


    I have a series of alphanumeric strings in the following format:

    AAA #A

    where the trailing alpha character may actually be one to three characters in lenght. The problem is that instead of a space between the two portions of the string, sometimes a / or - (or no space at all) has been inserted. How can I replace those characters with a space, or insert a space if none exists?

    I've come up with the following, but it doesn't work. Suggestions?

    ^([A-Za-z]{3}[/]?\d[A-Za-z]+)
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    Your regexp should catch two or three groups separately. You are trying to catch the whole and it is not clear what you will do with it later.

    Code:
    ([A-Za-z]+)[\s\-\/]+(\d+)([A-Za-z]+)
    
    replace with
    
    "$1 $2$3" or "\1 \2\3"
    
    depending on your language
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    You could also simplify and just accept ANYTHING before the #
    Code:
    /([A-Z]{3}).*(#[A-Z]+)/
    And replace it with:
    Code:
    $1 $2
    Now how you get $1 and $2 are up to you.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo