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

    Join Date
    Oct 2013
    Rep Power

    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?

  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Saint-Petersburg, Russia
    Rep Power
    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.

    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
    Pennsylvania, USA
    Rep Power
    You could also simplify and just accept ANYTHING before the #
    And replace it with:
    $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