10
回答
java如何将字符串的所有括号中指定字符替换掉?(正则表达式或者其它方法)
注册华为云得mate10,2.9折抢先购!>>>   
要处理字符串 
Medical Care (  Costs Inverse##agonism( Costs Inverse##agonism) ) Costs (Inverse ## agonism )at beta_1 ## adrenergic receptors
替换操作:将字符串中 所有括号 ##替换成 @
处理结果:
Medical Care (  Costs Inverse@agonismCosts Inverse@agonism) ) Costs (Inverse@agonism )at beta_1 ##adrenergic receptors
各路高手帮帮忙(正则表达式或者其它方法都行的)
举报
Kwin
发帖于4年前 10回/12K+阅

以下是问题补充:

  • @Kwin :上面字符串要替换掉 所有被( ) 与 { } 与 [ ] 包括的##号,各位大神再帮帮忙呀······ (4年前)
共有10个答案 最后回答: 4年前
String str = "Medical Care (  Costs Inverse##agonism( Costs Inverse##agonism) ) Costs (Inverse ## agonism )at beta_1 ## adrenergic receptors "; 
		Pattern p = Pattern.compile("\\((.*)\\)"); 
		Matcher m = p.matcher(str); 
		String temp = null;
		StringBuffer sb = new StringBuffer();
		while(m.find()){
			temp = "("+m.group(1)+")";
			temp = temp.replace("##", "@");
			m.appendReplacement(sb, temp);
		 }
		m.appendTail(sb);
		System.out.println(sb.toString());



--- 共有 2 条评论 ---
Timco你说的是对的,没法处理嵌套情况-.- 4年前 回复
johnchou该程序在字符串末尾加上(Inverse ## agonism )会把beta_1后的##也替换成@。 4年前 回复

就是 ## 换为了 @ 

java的String对象自带有replaceAll方法。

String a = a.replaceAll("需被替换的字符","新字符");




public static void main(String[] args)
{
	String s = "Medical Care (  Costs Inverse##agonism( Costs Inverse##agonism) ) Costs (Inverse ## agonism )at beta_1 ## adrenergic receptors";
	StringBuffer sb = new StringBuffer();
	boolean b = false;
	int t = 0;
	for (int i = 0; i < s.length(); i++)
	{
		char c = s.charAt(i);
		if (c == '(')
		{
			t++;
		}
		if (c == ')')
		{
			t--;
		}
		if (c != '#' || t == 0)
		{
			if (b)
			{
				sb.append('#');
			}
			sb.append(c);
			b = false;
		}
		else
		{
			if (b)
			{
				sb.append('@');
				b = false;
			}
			else
			{
				b = true;
			}
		}
	}
	System.out.println(sb.toString());
}

有时间没写自动机程序了,看看是否满足你的应用;



引用来自“Timco”的答案

String str = "Medical Care (  Costs Inverse##agonism( Costs Inverse##agonism) ) Costs (Inverse ## agonism )at beta_1 ## adrenergic receptors "; 
		Pattern p = Pattern.compile("\\((.*)\\)"); 
		Matcher m = p.matcher(str); 
		String temp = null;
		StringBuffer sb = new StringBuffer();
		while(m.find()){
			temp = "("+m.group(1)+")";
			temp = temp.replace("##", "@");
			m.appendReplacement(sb, temp);
		 }
		m.appendTail(sb);
		System.out.println(sb.toString());



太给力了!非常感谢!

引用来自“空杯子”的答案

public static void main(String[] args)
{
	String s = "Medical Care (  Costs Inverse##agonism( Costs Inverse##agonism) ) Costs (Inverse ## agonism )at beta_1 ## adrenergic receptors";
	StringBuffer sb = new StringBuffer();
	boolean b = false;
	int t = 0;
	for (int i = 0; i < s.length(); i++)
	{
		char c = s.charAt(i);
		if (c == '(')
		{
			t++;
		}
		if (c == ')')
		{
			t--;
		}
		if (c != '#' || t == 0)
		{
			if (b)
			{
				sb.append('#');
			}
			sb.append(c);
			b = false;
		}
		else
		{
			if (b)
			{
				sb.append('@');
				b = false;
			}
			else
			{
				b = true;
			}
		}
	}
	System.out.println(sb.toString());
}

有时间没写自动机程序了,看看是否满足你的应用;



非常感谢,这个够用了!!

刚上班写的一个:

String str = "Medical Care (  Costs Inverse##agonism( Costs Inverse##agonism) ) Costs (Inverse ## agonism )at beta_1 ## adrenergic receptors (Inverse ## agonism ) beta_1 ## adrenergic receptors";
        StringBuilder tempstr = new StringBuilder(str);
        Matcher m = Pattern.compile(".*(\\(.*##.*?\\)).*").matcher(str);
        List<Object> posList = new ArrayList<Object>();
        while (m.matches()) {
            Integer pos = str.indexOf(m.group(1));
            posList.add(str.indexOf("##", pos));
            while (pos++ < str.length()) {
                Integer opos = str.indexOf(m.group(1), pos);
                if (opos >= 0) {
                    posList.add(str.indexOf("##", opos));
                    pos = opos;
                } else {
                    break;
                }
            }
            str = str.replace(m.group(1), "");
            m = Pattern.compile(".*(\\(.*##.*?\\)).*").matcher(str);
        }
        Object[] posArr = posList.toArray();
        Arrays.sort(posArr);
        posList = Arrays.asList(posArr);
        for (int i = 0; i < posList.size(); i++) {
            tempstr.insert(Integer.valueOf(posList.get(i).toString()) + i * 1, "@");
        }
        System.out.println(tempstr.toString().replace("@##", "@"));

引用来自“Timco”的答案

String str = "Medical Care (  Costs Inverse##agonism( Costs Inverse##agonism) ) Costs (Inverse ## agonism )at beta_1 ## adrenergic receptors "; 
		Pattern p = Pattern.compile("\\((.*)\\)"); 
		Matcher m = p.matcher(str); 
		String temp = null;
		StringBuffer sb = new StringBuffer();
		while(m.find()){
			temp = "("+m.group(1)+")";
			temp = temp.replace("##", "@");
			m.appendReplacement(sb, temp);
		 }
		m.appendTail(sb);
		System.out.println(sb.toString());



纳尼,这个还真没注意。。。
综合各位意见,终于找到了好的方法:
public void replaceWord(){
String str = "Medical Care (  Costs Inverse##agonism( Costs Inverse##agonism) ) Costs (Inverse ## agonism )at beta_1 ## adrenergic receptors (Inverse ## agonism ) beta_1 ## adrenergic receptors";
     String [] patterns = {"\\([^)]+\\(", "\\)[^(]+\\)","\\([^()]+\\)"};
     for (int i = 0; i < patterns.length; i++) {
        Matcher matcher = Pattern.compile(patterns[i]).matcher(str);
        while(matcher.find()){
           str = str.replace(matcher.group(), matcher.group().replaceAll("##", "@"));
        }
     }
     System.out.println( str);
}

顶部