Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何替换行首集合中的所有字符?_Regex_Perl_Sed - Fatal编程技术网

Regex 如何替换行首集合中的所有字符?

Regex 如何替换行首集合中的所有字符?,regex,perl,sed,Regex,Perl,Sed,我有这样的台词: ,,,,,,,,,foo,bar ,,,,,x,,blabla, 使用正则表达式,是否可以仅用相同数量的空格替换行首的逗号?如果可以,如何替换?使用Java,并使用正向查找: String s = ",,,,,,,,,foo,bar"; System.out.println(s.replaceAll("(?<=^,+),", " ")); // ' foo,bar' sed示例: $ echo ',,,,,,,,,foo,bar ,,,,,x,,bla

我有这样的台词:

,,,,,,,,,foo,bar
,,,,,x,,blabla,

使用正则表达式,是否可以仅用相同数量的空格替换行首的逗号?如果可以,如何替换?

使用Java,并使用正向查找:

String s = ",,,,,,,,,foo,bar";
System.out.println(s.replaceAll("(?<=^,+),", " "));
// '         foo,bar'

sed
示例:

$ echo ',,,,,,,,,foo,bar
,,,,,x,,blabla,' | sed ':r;s/^\(,*\),/\1 /;tr'
         foo,bar
     x,,blabla,

使用
perl

perl -pe 's/^,+/q{ } x length $&/e' <<< ',,,,,,,,,foo,bar'

perl-pe的/^,+/q{}x length$&/e'perl或sed在本例中,但使用一种语言我可以理解它。我只是想知道是否有一个纯粹的基于正则表达式的解决方案。
perl -pe 's/^,+/q{ } x length $&/e' <<< ',,,,,,,,,foo,bar'