C# Regexp,强制文本垂直

C# Regexp,强制文本垂直,c#,regex,C#,Regex,我有这样一个文本:“我喜欢stackoverflow”,并希望这个结果与regexp(用\n分隔): 我怎么能用c#做到这一点呢?我想你要找的正则表达式是 (a-zA-Z) 取代 $1\n此操作不需要正则表达式,只需执行以下操作: string input = "i like stackoverflow"; string result = string.Join("\n", input.Replace(" ", "").ToCharArray()); 此代码执行以下操作: 删除字符串中的所有

我有这样一个文本:“我喜欢stackoverflow”,并希望这个结果与regexp(用
\n
分隔):


我怎么能用c#做到这一点呢?

我想你要找的正则表达式是

(a-zA-Z)

取代


$1\n

此操作不需要正则表达式,只需执行以下操作:

string input = "i like stackoverflow";

string result = string.Join("\n", input.Replace(" ", "").ToCharArray());
此代码执行以下操作:

  • 删除字符串中的所有空格(
    input.Replace(“,”)
  • 将字符串拆分为字符数组(
    .tocharray()
  • 将字符数组中的元素重新连接到单个字符串中,并用换行符分隔每个元素(
    string.Join(“\n”,…)
  • 当正则表达式的使用得到保证时,它是非常有用的。但如果不是这样,请记住:

    有些人在遇到问题时会想“我知道,我会使用正则表达式。”现在他们有两个问题


    我真的不确定正则表达式是否适合(请参阅),但如果您真的想要一个正则表达式

    Regex.Replace("i like stackoverflow", "([^\\s]\\s?)", "$1\n");
    
    rot90 如果你能把它翻译成C♯, 我想它可以做你想做的:

    #!/usr/bin/perl 
    # rot90 
    # Tom Christiansen <tchrist@perl.com>
    
    $/ = "";
    
    # uncomment for easier to read, but not reversible:
    ### @ARGV = map { "fmt -20 $_ |" } @ARGV;
    
    while ( <> ) {
        chomp;
        @lines = split /\n/;
        $MAXCOLS = -1;
        for (@lines) { $MAXCOLS = length if $MAXCOLS < length; }
        @vlines = ( " " x @lines ) x $MAXCOLS;
        for ( $row = 0; $row < @lines; $row++ ) {
            for ( $col = 0; $col < $MAXCOLS; $col++ ) {
                $char = ( length($lines[$row]) > $col  )
                        ? substr($lines[$row], $col, 1) 
                        : ' ';
                substr($vlines[$col], $row, 1) = $char;
            }
        }
        for (@vlines) {
            # uncomment for easier to read, but again not reversible
            s/(.)/$1 /g;
            print $_, "\n";
        }
        print "\n";
    }
    
    它生成以下输出文件:

    I f o I o a a t I f o s w w t e 
      i f   f n n h   i f h h i h v 
    s r   h   d d a s r   a e t a e 
    i e a a b     t i e h l n h t r 
    t   l v u m p   t   o l   o     
      a l e t e l I   a w   w u I s 
    b n     t a a ' b n   b i t   e 
    e d t s e d c v e d t e n   s e 
    s   h e r o e e s   h   t a h . 
    i t a e f w s   i t e   e   a   
    d h t n l f   b d h     r s l   
    e i   : i l   e e i w     p l   
      n     e o   e   n o   c r     
    t k     s w   n t k r   o i     
    h         e   . h   l   m n     
    e         r     e   d   e g     
              s             s       
    
    希望这有帮助!如果你把它翻译成C♯, 如果你能寄给我一份,我会很高兴的,因为我很好奇它在那里是如何工作的。我只懂Java(*),不懂C♯, 我听说这里好多了。再说一次,头上的靴子也是一样。:)


    • 我不是说我只知道Java语言。我曾在BASIC-PLUS、FORTRAN、Pascal、Modula-2、PDP-11汇编程序、C、C中工作过⁺⁺, DCL、Exec8、Ada、Lisp、Scheme、Prolog、Ratfor、Shell、Icon、REXX、Awk、Perl、Python、M4、Tcl、Ruby、Java、Go,还有我自己设计的十几种语言。而且我肯定我忘了一些。我只是不知道C♯. 请原谅我。我希望我的代码仍然可以帮助你
    #!/usr/bin/perl 
    # rot90 
    # Tom Christiansen <tchrist@perl.com>
    
    $/ = "";
    
    # uncomment for easier to read, but not reversible:
    ### @ARGV = map { "fmt -20 $_ |" } @ARGV;
    
    while ( <> ) {
        chomp;
        @lines = split /\n/;
        $MAXCOLS = -1;
        for (@lines) { $MAXCOLS = length if $MAXCOLS < length; }
        @vlines = ( " " x @lines ) x $MAXCOLS;
        for ( $row = 0; $row < @lines; $row++ ) {
            for ( $col = 0; $col < $MAXCOLS; $col++ ) {
                $char = ( length($lines[$row]) > $col  )
                        ? substr($lines[$row], $col, 1) 
                        : ' ';
                substr($vlines[$col], $row, 1) = $char;
            }
        }
        for (@vlines) {
            # uncomment for easier to read, but again not reversible
            s/(.)/$1 /g;
            print $_, "\n";
        }
        print "\n";
    }
    
    I sit beside the
    fire and think
    of all that
    I have seen:
    of butterflies
    and meadowflowers
    and places
    that I've been.
    I sit beside the
    fire and think
    of how the world
    shall be
    when winter comes
    without a spring
    that I shall
    ever see.
    
    I f o I o a a t I f o s w w t e 
      i f   f n n h   i f h h i h v 
    s r   h   d d a s r   a e t a e 
    i e a a b     t i e h l n h t r 
    t   l v u m p   t   o l   o     
      a l e t e l I   a w   w u I s 
    b n     t a a ' b n   b i t   e 
    e d t s e d c v e d t e n   s e 
    s   h e r o e e s   h   t a h . 
    i t a e f w s   i t e   e   a   
    d h t n l f   b d h     r s l   
    e i   : i l   e e i w     p l   
      n     e o   e   n o   c r     
    t k     s w   n t k r   o i     
    h         e   . h   l   m n     
    e         r     e   d   e g     
              s             s