Replace multiple capture groups using regexp with java

I have this requirement - for an input string such as the one shown below

8This8 is &reallly& a #test# of %repl%acing% %mul%tiple 9matched9 9pairs

I would like to strip the matched word boundaries (where the matching pair is 8 or & or % etc) and will result in the following

This is really a test of repl%acing %mul%tiple matched 9pairs

This list of characters that is used for the pairs can vary eg 8,9,%,# etc and only the words matching the start and end with each type will be stripped of those characters, with the same character embedded in the word remaining where it is.

Using Java I can do a pattern as b8([^s]*)8b and replacement as $1, to capture and replace all occurrences of 8...8, but how do I do this for all the types of pairs?

I can provide a pattern such as b8([^s]*)8b|b9([^s]*)9b .. and so on that will match all types of matching pairs *8,9,..), but how do I specify a 'variable' replacement group -

eg if the match is 9...9, the the replacement should be $2.

I can of course run it through multiple of these, each replacing a specific type of pair, but I am wondering if there is a more elegant way.

Or is there a completely different way of approaching this problem?

Thanks.


You could use the below regex and then replace the matched characters by the characters present inside the group index 2.

(?<!S)(S)(S+)1(?=s|$)

OR

(?<!S)(S)(S*)1(?=s|$)

Java regex would be,

(?<!S)(S)(S+)1(?=s|$)

DEMO

String s1 = "8This8 is &reallly& a #test# of %repl%acing% %mul%tiple 9matched9 9pairs";
System.out.println(s1.replaceAll("(?<!S)(S)(S+)1(?=s|$)", "$2"));

Output:

This is reallly a test of repl%acing %mul%tiple matched 9pairs

Explanation:

  • (?<!S) Negative lookbehind, asserts that the match wouldn't be preceded by a non-space character.
  • (S) Captures the first non-space character and stores it into group index 1.
  • (S+) Captures one or more non-space characters.
  • 1 Refers to the character inside first captured group.
  • (?=s|$) And the match must be followed by a space or end of the line anchor.
  • This makes sure that the first character and last character of the string must be the same. If so, then it replaces the whole match by the characters which are present inside the group index 2.
  • For this specific case, you could modify the above regex as,

    String s1 = "8This8 is &reallly& a #test# of %repl%acing% %mul%tiple 9matched9 9pairs";
    System.out.println(s1.replaceAll("(?<!S)([89&#%])(S+)1(?=s|$)", "$2"));
    

    DEMO


    (?<![a-zA-Z])[8&#%9](?=[a-zA-Z])([^s]*?)(?<=[a-zA-Z])[8&#%9](?![a-zA-Z])
    

    Try this.Replace with $1 or 1 .See demo.

    https://regex101.com/r/qB0jV1/15

    (?<![a-zA-Z])[^a-zA-Z](?=[a-zA-Z])([^s]*?)(?<=[a-zA-Z])[^a-zA-Z](?![a-zA-Z])
    

    Use this if you have many delimiters.

    链接地址: http://www.djcxy.com/p/82938.html

    上一篇: Django错误报告电子邮件:env vars泄漏信息

    下一篇: 用java将regexp替换多个捕获组