Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
如何生成可生成原始文本和修改版本的regexp?_Regex - Fatal编程技术网

如何生成可生成原始文本和修改版本的regexp?

如何生成可生成原始文本和修改版本的regexp?,regex,Regex,如何构建一个正则表达式来保留原始文本,并附加原始文本的修改版本?例如,在“|”之后添加以下文本的副本,删除非字母数字字符,并用破折号替换空格 这: 变成这样: Hi. My name is Nick.|Hi-My-name-is-Nick This: Is the second line?|This-Is-the-second-line 编辑:现在消除了OP指定的标点符号。这也正确地将下划线视为标点符号,使用正则表达式(\W |),而不仅仅是\W(感谢@crayons>) 这就是如何在Jav

如何构建一个正则表达式来保留原始文本,并附加原始文本的修改版本?例如,在“|”之后添加以下文本的副本,删除非字母数字字符,并用破折号替换空格

这:

变成这样:

Hi. My name is Nick.|Hi-My-name-is-Nick
This: Is the second line?|This-Is-the-second-line
编辑:现在消除了OP指定的标点符号。这也正确地将下划线视为标点符号,使用正则表达式
(\W |)
,而不仅仅是
\W
(感谢@crayons>)


这就是如何在Java中使用regex实现的。我没有使用regex进行复制。相反,我只是将原始字符串(加上“|”)附加到输出缓冲区,然后再附加替换字符串

import  java.util.regex.Pattern;
import  java.util.regex.Matcher;

/**
   <P>{@code java DupLinesChangeSpaceToDashInDup}</P>
 **/
public class DupLinesChangeSpaceToDashInDup  {
   public static final void main(String[] ignored)  {
      //input
         String sLS = System.getProperty("line.separator", "\r\n");
         StringBuilder inputBldr = new StringBuilder().
            append("Hi. My name is Ni__ck.").append(sLS).
            append("This is the second line!").append(sLS);
         String[] textInputArr = inputBldr.toString().split(sLS);

      //config
         //"": Dummy search string, to reuse matcher
         Matcher mtchrNonWordChar = Pattern.compile("(\\W|_)").matcher("");

      //go
         StringBuffer rplcmntBfr = new StringBuffer();
         for(String lineText : textInputArr)  {
            rplcmntBfr.append(lineText).append("|");

            String[] wordArr = lineText.split(" ");

            for(int i = 0; i < wordArr.length; i++)  {
               String s = wordArr[i];
               rplcmntBfr.append(mtchrNonWordChar.reset(s).replaceAll(""));
               if(i < wordArr.length - 1)  {
                  rplcmntBfr.append("-");
               }
            }
            rplcmntBfr.append(sLS);
         }

      System.out.println(rplcmntBfr);
   }
}

。。。这是php版本

$string = "Hi. My name is Nick.";
$string = $string.'|'.preg_replace('~\s~','-',preg_replace('~[^a-z0-9\s]~i','',$string));
。。。还有一个js版本

var string = "Hi. My name is Nick.";
string = string+'|'+string.replace(/[^a-z0-9\s]/gi,'').replace(/\s/g,'-');
Javascript版本:

var s = "Hi. My name is Nick.\nThis is the second line!\n";
var replaced = s.replace(/.+/g, function(m) {
  return m + '|' + m.replace(/[^a-z0-9\s]+/ig, '').replace(/\s+/g, '-');
} );
console.log(replaced);
输出:

Hi. My name is Nick.|Hi-My-name-is-Nick
This is the second line!|This-is-the-second-line
保留原文

这个步骤很简单,因为您可以将结果附加到原始文本中。如果您的意思是“保留原始匹配项”,则在匹配项周围加上大括号
()
,这将是组1,由\1或$1引用,具体取决于语言

附加一个。。。删除非字母数字字符,空格替换为破折号

这最容易通过两个步骤完成:

1)用破折号替换空格

匹配=
\s

替换=
-

这将用
hello how------you

如果要用单个
-
替换连续空格,请使用

匹配=
\s+
替换=
-

2)删除非字母数字

匹配=
[^A-Za-z0-9]


Replace=
'

这是使用Perl的方法

全文共分三部分

1) 打印原始字符串,后跟一个
|

print "$_|";
2) 用单个
-
替换非字母数字字符

 s/[^a-zA-Z0-9]+/-/g
3) 使用删除最后一个
-

s/[-]$//
一些例子-

$ echo -n "Hi. My name is Nick." | perl -pe 'print "$_|"; s/[^a-zA-Z0-9]+/-/g' 
  | perl -pe 's/[-]$//'
Hi. My name is Nick.|Hi-My-name-is-Nick


$ echo -n "This: Is the second line?" | perl -pe 'print "$_|"; s/[^a-zA-Z0-9]+/-/g' 
  | perl -pe 's/[-]$//'
This: Is the second line?|This-Is-the-second-line

通常使用捕获组,然后用
\1
$1
引用它(其中1是捕获组的编号)。如果您提到这是哪种语言,会更有帮助。
变成了这
的第二行,而不是这
的第二行!?他要求去掉非字母数字(不包括空格)。谢谢!我没听清楚。需要一种完全不同的(幸运的是更简单、更短)方法。更新。关于这件事的几点注意事项。。1) 假设非字母数字的内容将以空格作为前缀或后缀。因此,这将不正确地将“foo.bar”替换为“foo-bar”,而不是返回“foobar”。2) 关于字母数字的注释。。他说的是字母数字,而不是“单词”字符。所以
\W
将保留“foo\u bar”,当它应该返回“foobar”时,感谢您提供有关下划线的提示。我已经用regex
(\W |)
@crayon更新了我的Java答案,谢谢你的评论。我相应地更新了答案。
s/[-]$//
$ echo -n "Hi. My name is Nick." | perl -pe 'print "$_|"; s/[^a-zA-Z0-9]+/-/g' 
  | perl -pe 's/[-]$//'
Hi. My name is Nick.|Hi-My-name-is-Nick


$ echo -n "This: Is the second line?" | perl -pe 'print "$_|"; s/[^a-zA-Z0-9]+/-/g' 
  | perl -pe 's/[-]$//'
This: Is the second line?|This-Is-the-second-line