0
^((?!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];
}
elemakil
  • 3,681
  • 28
  • 53
diwakar
  • 1
  • 4
  • 3
    Possible duplicate of [Splitting a Java String by the pipe symbol using split("|")](http://stackoverflow.com/questions/10796160/splitting-a-java-string-by-the-pipe-symbol-using-split) – Wiktor Stribiżew Oct 19 '16 at 07:35
  • 2
    Actually, you can just use a `substring` since the number of chars you need to remove is fixed on both ends. – Wiktor Stribiżew Oct 19 '16 at 07:36
  • Indeed, you don't need to `split` this string, as there is only one part that you need to extract from it. `split` is for cases where the string is a chain of parts that you need, and those parts are separated by some delimiter or pattern. – RealSkeptic Oct 19 '16 at 07:43
  • your problem is that `contains` basically search for the argument in the string whereas `split` evaluate the argument as as regex. In a regex, `^`,`(`, `?` are specials characters – jhamon Oct 19 '16 at 07:45

5 Answers5

2

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());
Deepak M.
  • 68
  • 6
1

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,"");
0

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]","");
Jay Prakash
  • 787
  • 6
  • 22
0

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.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
-1

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 ?

Sigar Dave
  • 2,598
  • 1
  • 20
  • 42