Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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
如何替换“替换”:abc,cde\t“;“至”;,abc“cde”;在Java中使用正则表达式?_Java_Regex - Fatal编程技术网

如何替换“替换”:abc,cde\t“;“至”;,abc“cde”;在Java中使用正则表达式?

如何替换“替换”:abc,cde\t“;“至”;,abc“cde”;在Java中使用正则表达式?,java,regex,Java,Regex,我有如下字符串列表:(不带引号) “:abc\t” :abc,cde\t :abc、efg、cde\t :abc,cde\t 希望将其转换为: "<someother string without :>|abc\t<some other string without \t>" "<someother string without :>|abc|cde\t<some other string without \t>" "<someother

我有如下字符串列表:(不带引号)

“:abc\t”
:abc,cde\t
:abc、efg、cde\t
:abc,cde\t
希望将其转换为:

"<someother string without :>|abc\t<some other string without \t>"
"<someother string without :>|abc|cde\t<some other string without \t>"
"<someother string without :>|abc|efg|cde\t<some other string without \t>"
"<someother string without :>|abc|cde\t<some other string without \t>"

“|abc\t”
“| abc | cde\t”
“| abc | efg | cde\t”
“| abc | cde\t”
我想知道这是否可行


谢谢

除非您多次应用它,否则不要认为您可以用regex实现这一点。您可以这样做:

public static String convert(String s) {
    int start = s.indexOf(':') + 1;
    int end = s.indexOf('\t', start);

    return s.substring(0, start)
            + s.substring(start, end).replaceAll(",", "|")
            + s.substring(end, s.length());
}
试试这个:

public class T28Regex {
public static void main(String[] args) {
    String[] strings = { "<someother string without *>:abc\t<some other string without \t>",
            "<someother string without *>:abc,cde\t<some other string without \t>",
            "<someother string without *>:abc,efg,cde\t<some other string without \t>",
            "<someother string without *>:abc,cde\t<some other string without \t>" };

    for (String s : strings) {
        System.out.println(s.substring(0, s.indexOf(":")) + "|"
                + s.substring(s.indexOf(":") + 1, s.indexOf("\t", s.indexOf(":"))).replaceAll(",", "|")
                + s.substring(s.indexOf("\t", s.indexOf(":"))));
    }
}
}
公共类T28Regex{
公共静态void main(字符串[]args){
String[]字符串={“:abc\t”,
“:abc,cde\t”,
“:abc、efg、cde\t”,
“:abc,cde\t”};
用于(字符串s:字符串){
System.out.println(s.substring(0,s.indexOf(“:”)+“|”
+s.substring(s.indexOf(“:”)+1,s.indexOf(“\t”,s.indexOf(“:”).replaceAll(“,”,“|”)
+s.substring(s.indexOf(“\t”,s.indexOf(“:”));
}
}
}
试试这个

function Replace_(str ) {
  var patt = /(:)((([\w]*(,)?)){2,})(\\t<)/gi;
  var res = str.replace(patt, function($1,$2,$3){
  return $1.replace(/,/g, "|").replace(":", "|");
  });
return res;
}
函数替换(str){

var patt=/(:)(([\w]*(,)?){2,})(\\t您的输入是否包含您不想替换的逗号?如果不包含,这应该相对简单:
(?):\b,\b
谢谢,我在函数中而不是正则表达式谢谢,我在函数中而不是正则表达式中
function Replace_(str ) {
  var patt = /(:)((([\w]*(,)?)){2,})(\\t<)/gi;
  var res = str.replace(patt, function($1,$2,$3){
  return $1.replace(/,/g, "|").replace(":", "|");
  });
return res;
}