简体   繁体   中英

Split a String with multiple special Characters in it - Java

^((?!PATTERN).)*$

If the above string is given the output should be PATTERN.

The special characters are the same for every input, only the words inside those special characters can be changed by the user.

When i do a split i get an Unclosed group near index 6 Exception.

String test = "^((?!PATT).)*$";
String patternOne = "^((?!";
String patternTwo = ").)*$";
if(test.contains(patternOne) && test.contains(patternTwo))
{
  test = test.split(patternOne)[1];
  test = test.split(patternTwo)[0];
}

The split() method on String takes a RegEx. What you are passing is an invalid RegEx. You are better off using the substring() function as you already know the prefix and suffix pattern.

test = test.substring(patternOne.length(), test.length() - patternTwo.length());

shouldn't this be the easier way ? since you are overriding your test variable anyways you could just replace your patterns with nothing. and you would not need to check if test contains them at all

    String test = "^((?!PATT).)*$";
    String patternOne = "^((?!";
    String patternTwo = ").)*$";
    test = test.replace(patternOne, "").replace(patternTwo,"");

Please change code :

String test = "^((?!PATT).)*$";
String patternOne = "^((?!";
String patternTwo = ").)*$";
if(test.contains(patternOne) && test.contains(patternTwo))
{
  test = test.split(patternOne)[1];
  test = test.split(patternTwo)[0];
}

To like this it is more easy:

String test ="^((?!PATTERN).)*$";
String  result = test.replaceAll("[^\\w\\s]","");

The reason you get the error is that the split method understands its argument as a regex , and all the characters in your prefix and suffix are actually special characters in regex.

There are ways to escape special characters, but in this case, there is really no need to use the regex-based split method for this particular requirement. It is actually not the right tool. Just extract the word from within the pattern using substring , as you know exactly where it starts and where it ends:

class Test {
    public static final String PREFIX = "^((?!";
    public static final int PREFIX_LEN = PREFIX.length();
    public static final String SUFFIX = ").)*$";
    public static final int SUFFIX_LEN = SUFFIX.length();

    public static String extractWord( String arg ) {
        if (arg.startsWith(PREFIX) && arg.endsWith(SUFFIX)) {
            return arg.substring(PREFIX_LEN, arg.length() - SUFFIX_LEN);
        }
        return null;
    }

    public static void main( String[] args ) {
        System.out.println( extractWord("^((?!PATT).)*$") );
    }
}

This tells it to extract the part of the string that starts after the PREFIX ends, and ends at the beginning of the SUFFIX.

You can achieve it by splitting the string mulitple times

String test = "^((?!PATT).)*$";
String patternOne = "^((?!";
String patternTwo = ").)*$";
if(test.contains(patternOne) && test.contains(patternTwo))
{
  test = test.split(patternOne)[1].split(patternTwo)[0];
}

Let me know if this works or not ?

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