简体   繁体   中英

Remove matching String using regular Expression in java

This is my code please check. At the End i want to remove list-style-image: url(images/dot.gif); from the String

String temp = "font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: F143F;list-style-image: url(images/dot.gif);list-style-type: none;"; 

Pattern pxPattern = Pattern.compile("([a-z]+-)+([a-z]+):(\\s)url\\(.*?\\);");

Matcher pxMatcher = pxPattern.matcher(temp);

while(pxMatcher.find()) {
    System.out.println(pxMatcher.group());
    String urlString =pxMatcher.group();
    if(!urlString.matches("http://|https://")) {
        System.out.println("Firts: "+temp.trim());
        System.out.println(urlString);
        System.out.println(temp.replaceAll(urlString, ""));
        //System.out.println("Remove: "+temp);
    }
}

This is a general answer to the question title; it may not directly address the specifics of the question. Let's say we have a string called PATTERN and a string called body. Then we can remove all matches of PATTERN from body as follows:

StringBuilder builder = new StringBuilder();
int x = 0;
Matcher m = Pattern.compile(PATTERN).matcher(body);
while (m.find()) {
  builder.append(body.substring(x, m.start()));
  x = m.end();
}
return(builder.toString());

Eg if PATTERN = "XOX" and body = "Hello XOXWorldXOX" then we should get back "Hello World".

How it works: iterate through each match, recording the index in the string just after the last match, and adding the substring from that index to the start of the current match to a string builder, then skipping the index forward over the current match to the end. Finally, build the string.

Note: The answer of beny23 is better for removal of a regex from a string. However, with a small tweak, the above code can be made more general. It can be changed to replace each subsequent occurrence of the regex with a unique replacement string. This is more powerful and general than replaceAll, but it's an odd corner case that probably doesn't crop up that often. Still, to show you what I mean, suppose instead of removing each regex match, we want to replace the first match with "match_1" and the second with "match_2" and so on, we can do this:

StringBuilder builder = new StringBuilder();
int x = 0;
int matchNumber = 1;
Matcher m = Pattern.compile(PATTERN).matcher(body);
while (m.find()) {
  builder.append(body.substring(x, m.start()));
  builder.append("match_" + matchNumber);
  x = m.end();
}
return(builder.toString());

Eg if PATTERN = "XOX" and body = "Hello XOXWorldXOX" then we should get back "Hello match_1Worldmatch_2".

With a little more tweaking, we could generalise the above to replace each subsequent match with an array element, making it truly general.

It works for me fine

while(pxMatcher.find()) {
    System.out.println(pxMatcher.group());
    String urlString =pxMatcher.group();
    if(!urlString.matches("http://|https://")) {
        System.out.println("Firts: "+temp.trim());
        System.out.println(urlString);
        temp = temp.replace(urlString, "");
        System.out.println("Remove: "+temp);
     }
}

Result is

list-style-image: url(images/dot.gif);
Firts: font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: F143F;list-style-image: url(images/dot.gif);list-style-type: none;
list-style-image: url(images/dot.gif);
Remove: font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: F143F;list-style-type: none;

I would remove the list-style-image as follows (rather than using a while loop, this can be done in one line):

temp.replaceAll("list-style-image:[^;]+;?", "");

To explain:

  • This will look for list-style-image ,
  • then one or more characters which aren't a semicolon
  • then an optional semicolon

This will remove the list-style-image attribute from the middle as well as the end of your string.

Result:

font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: F143F;list-style-type: none; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM