开发者

Replace comma in parentheses using regex in java

开发者 https://www.devze.com 2023-01-16 02:03 出处:网络
I want to replace comma when its insideparentheses only. For Example Progamming languages (Java, C#, Perl)

I want to replace comma when its inside parentheses only.

For Example

 Progamming languages (Java, C#, Perl)

TO

Progamming languages (Java or C# or Perl)

but it should not repace comma in following string

Progamming languages Java, C#, Perl

CODE

It will replace correctly but its not matching up.

 String test  = "Progamming 开发者_开发百科languages (Java, C#, Perl)";
 String test1 = "Progamming languages Java, C#, Perl"


 String foo = replaceComma(test);
 String foo1 = replaceComma(test1);


 private static String replaceComma(String test)
 {
    String patternStr= "\\((?:.*)(,)(?:.*)\\)";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher= pattern.matcher(test);

    if(matcher.matches())
    {
      return  test.replaceAll("(,)", " or ");
    }

    return  test;

 }

UPDATE

String.replaceAll("(,)", " or "); will not work when you have string like this

String test = "Learning, languages (Java, C#, Perl)";

so you have to use @polygenelubricants code


You can use positive lookahead (?=…) like this:

    String COMMA_INSIDE = ",(?=[^()]*\\))";
    String text = "a, b, c, (d, e, f), g, h, (i, j, k)";
    System.out.println(
        text.replaceAll(COMMA_INSIDE, " OR")
    );
    // a, b, c, (d OR e OR f), g, h, (i OR j OR k)

This matches a comma, but only if the first parenthesis to its right is of the closing kind.

The [^…] is a negated character class. [^()] matches anything but parentheses. The * is zero-or-more repetition. The \) (written as "\\)" as a Java string literal) matches a closing parenthesis, literally. The backslash escapes what is otherwise a special metacharacter for grouping.

This assumes that the input string is well-formed, i.e. parentheses are always balanced and not nested.


Your error is using matches instead of find here:

if (matcher.find())

From the documentation:

  • The matches method attempts to match the entire input sequence against the pattern.
  • The find method scans the input sequence looking for the next subsequence that matches the pattern.

But your code also simplifies the issue somewhat - it replaces all commas even if only one of them is in parentheses. It's probably not a good idea to use regular expressions for this sort of task.

Instead you could scan the string one character at a time and count how many pairs of parentheses you are inside. When you see a ( increase the count, and when you see a ) decrease the count. If you see a , then check if the current count is zero.


Another approach is to use positive lookahead and positive lookbehind in your regular expression. That way you can search for commas that occur after a '(', but before a ')'.

(?=X) positive lookahead

(?<=X) positive lookbehind

Mark is correct you need a loop instead of replaceAll. But you can still use a regular expression using the technique I've described.


String  replaceComma(String input)
{
 String[] strSplit=input.split("[\\(\\)]");
  if(strSplit.length==2)
   input=input.replaceAll(", "," or ");
 return input;
}

UPDATE: For String's like "Learning, languages (Java, C#, Perl)"

String  replaceComma(String input)
{
String[] splitStr=input.split("[\\(\\)]");
 if(splitStr.length==2)
   input=input.replace(splitStr[1],splitStr[1].replaceAll(", ", " or "));
return input;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消