RegEx replace stuffing spaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Mittenzwey

    #1

    RegEx replace stuffing spaces

    Is there a way to do a Regex replace on a string to stuff some variable
    amount of spaces into a location of a string?
    I need an expression which would take an unknown length string and move the
    last char to position 15.

    EX:
    123456
    transforms to
    12345 6

    I have a start, but not sure how to do the middle part:
    match = "(.*)(.$)"
    replace = "$0--spaces go here--$1"


  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: RegEx replace stuffing spaces

    Hi Bill,

    What about constructing the regular expression dynamically, inserting the
    desired number of spaces on the fly? This might be not the best solution if
    RegularExpressi ons have some syntax to populate a number character but I
    beleive I haven't seen one.

    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "Bill Mittenzwey" <bmittenzwey@ho tmail.com> wrote in message
    news:OucLW6EjDH A.2336@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Is there a way to do a Regex replace on a string to stuff some variable
    > amount of spaces into a location of a string?
    > I need an expression which would take an unknown length string and move[/color]
    the[color=blue]
    > last char to position 15.
    >
    > EX:
    > 123456
    > transforms to
    > 12345 6
    >
    > I have a start, but not sure how to do the middle part:
    > match = "(.*)(.$)"
    > replace = "$0--spaces go here--$1"
    >
    >[/color]

    Comment

    • Eric Gunnerson [MS]

      #3
      Re: RegEx replace stuffing spaces

      If you use a MatchEvaluator function when you call Replace(), you can do
      this kind of thing fairly easily

      --
      Eric Gunnerson

      Visit the C# product team at http://www.csharp.net
      Eric's blog is at http://blogs.gotdotnet.com/ericgu/

      This posting is provided "AS IS" with no warranties, and confers no rights.
      "Bill Mittenzwey" <bmittenzwey@ho tmail.com> wrote in message
      news:OucLW6EjDH A.2336@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Is there a way to do a Regex replace on a string to stuff some variable
      > amount of spaces into a location of a string?
      > I need an expression which would take an unknown length string and move[/color]
      the[color=blue]
      > last char to position 15.
      >
      > EX:
      > 123456
      > transforms to
      > 12345 6
      >
      > I have a start, but not sure how to do the middle part:
      > match = "(.*)(.$)"
      > replace = "$0--spaces go here--$1"
      >
      >[/color]


      Comment

      Working...