【问题标题】:java regex negate with boundaries (square bracktes)java正则表达式否定边界(方括号)
【发布时间】:2018-06-08 17:40:05
【问题描述】:

如果有人可以帮助我解决 JAVA 正则表达式要求,我将不胜感激 我得到了一个类似"/ABC/KLM[XYZ/ABC/KLM]/ABC"的字符串

我想用方括号替换所有 ABC not。 在这种情况下,应该只找到第一个和最后一个 ABC。 但中间不是ABC,因为它是用方括号括起来的

【问题讨论】:

  • 你需要关心嵌套括号吗?
  • 会有随机的左括号或右括号,还是所有的左括号都有右括号
  • 这是一个单独使用正则表达式处理的丑陋问题。您可能应该使用解析器。
  • 最好取出[...] 中的字符串并在这些位置上放置一个标记。之后检查剩余的刺痛中的ABC 并根据需要更换它。然后再次用我们首先删除的相应字符串替换标记。
  • 创建一个堆栈。每次看到一个开括号[ 时,将其推入堆栈。每次你看到一个右括号] 从堆栈中弹出一个左括号。然后,在堆栈为空时替换ABC

标签: java regex


【解决方案1】:

如果没有递归正则表达式,您将无法做到这一点。 Java 在标准库中不支持这一点,但在 Perl 或 .NET 中发现的正则表达式风格支持。这与尝试匹配 HTML 标记中的内容本质上是相同的问题 - 到目前为止,最简单的方法是使用 基于堆栈的解析器

【讨论】:

    【解决方案2】:

    解决方案在这里:

        public class MergeParentAndChildXPATH {
    
    	public static void main(String[] args) {
    		
    		
    		String substringToBeFound = "ABC";
    		String toReplayceWith = "XXX";
    		String xPathFromExcel = "/UTILMD/ABC/[XYZ/ABC/KLM]KLM[XYZ/ABC/[XYZ/ABC/KLM]KLM]/ABC";
    		System.out.println("original    String\t"+xPathFromExcel);
    		String manupulatedString =	mergeParentAndChildXPATH(substringToBeFound, toReplayceWith,xPathFromExcel);
    		
    		System.out.println("manipulated String\t"+manupulatedString);
    		
    		
    	}
    
    	public static String  mergeParentAndChildXPATH(String substringToBeFound, String toReplayceWith, String xPathFromExcel  ) {
    		StringBuffer sbManipulatedString = new StringBuffer();
    		
    		int lengthABC = substringToBeFound.length();
    		CharStack charStack = new CharStack();
    		
    		
    		String substringAfterMatch = "";
    		while (xPathFromExcel.indexOf(substringToBeFound)>-1) {
    			int matchStartsAt = xPathFromExcel.indexOf(substringToBeFound); 
    			int matchEndssAt = xPathFromExcel.indexOf(substringToBeFound)+lengthABC;
    			String substringBeforeMatch = xPathFromExcel.substring(0, matchStartsAt);
    			substringAfterMatch = xPathFromExcel.substring(matchStartsAt+lengthABC);
    			String substringMatch = xPathFromExcel.substring(matchStartsAt, matchEndssAt);
    //			System.out.println("Loop Count\t"+loopCount);
    //			System.out.println("substringBeforeMatch\t"+substringBeforeMatch);
    //			System.out.println("substringAfterMatch\t"+substringAfterMatch);
    //			System.out.println("starts "+matchStartsAt+ " ends "+matchEndssAt);
    //			System.out.println("Output of match: "+substringMatch);
    			
    			// now tokenize the string till match is reached and memorize brackets via Stack
    			String sTokenize = xPathFromExcel;
    			for (int i = 0; i < matchStartsAt; i++) {
    				char ch = sTokenize.charAt(0);
    //				System.out.println(ch);
    //				System.out.println(sTokenize.substring(0,1));
    				if (ch == '[') {
    					charStack.push(ch);
    				}
    				if (ch == ']') {
    					charStack.pop();
    				}
    				sTokenize = sTokenize.substring(1);
    			}//for
    			if (charStack.empty()) {
    				
    				substringMatch = substringMatch.replaceAll(substringMatch, toReplayceWith);
    			}
    			// 
    			sbManipulatedString.append(substringBeforeMatch + substringMatch);
    //			System.out.println("manipulatedString\t"+sbManipulatedString.toString());
    			xPathFromExcel = substringAfterMatch;
    //			System.out.println("remaining String\t"+substringAfterMatch);
    			
    			
    		}
    		return (sbManipulatedString.toString()+substringAfterMatch);
    
    	}
    }
    
    import java.util.Stack;
    
    public class CharStack {
    
    	private Stack theStack;
    
    	CharStack() {
    		theStack = new Stack();
    	}
    
    	public char peek() {
    		Character temp = (Character) theStack.peek();
    		return temp.charValue();
    	}
    
    	public void push(char c) {
    		theStack.push(new Character(c));
    	}
    
    	public char pop() {
    		char temp = (Character) theStack.pop();
    		return temp;
    	}
    
    	public boolean empty() {
    		return theStack.empty();
    	}
    
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      相关资源
      最近更新 更多