Question: I'm trying to split String like:

`"Stack Overflow #forum #website";` -> `"Stack Overflow" "#forum #website"`

I tried to use split function like:`str.split("( )#", 2);`, but the output was like: `"Stack Overflow" "forum #website"`.

It seems that the `()` does not work, what should I do to fix this?

Thanks in advance.

Answer: 
You need to use positive lookahead assertion.

```
str.split("\\s(?=#)", 2);

```

*Little Explanation:*

* `\\s` matches any kind of space character.
* `(?=#)` Positive lookahead which asserts that the match ie , `space` should be followed by `#`.
* So space before `#forum` got matched and java do splitting according to that space character.

or

```
str.split(" (?=#)", 2);

```
```
public class split {

       public static void main(String args[]){
          String Str = new String("Stack Overflow #forum #website");
 //**********that's what is to be done *************************** 
          System.out.println("Return Value :" );
          for (String retval: Str.split("(?=#)", 2)){
             System.out.println(retval);
          }
 //***********THAT'S WHAT YOU WERE DOING ****************************** 
          System.out.println("");
          System.out.println("Return Value :" );
          for (String retval: Str.split("#", 2)){
             System.out.println(retval);
          }

       }

 }

```