Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regular expression to separate String taking care of paranthesis

Last post 10-31-2006, 12:05 PM by Sergei Z. 3 replies.
Sort Posts: Previous Next
  •  10-31-2006, 9:02 AM 23752

    Regular expression to separate String taking care of paranthesis

    Hi

    Can someone please help me to write a regular expression in Java which will split a String abc="Hello (good morning) world" into "Hello", "(good morning)", and "world".

    Currently, my expression is abc.split("\\s"); which splits on the basis of spaces only. I tried abc.split("\\s[^[(\\w\\s\\w)]]"); but that didnt work. 

    Thanks a lot for all the help

    Gem 

     

  •  10-31-2006, 10:30 AM 23755 in reply to 23752

    Re: Regular expression to separate String taking care of paranthesis

    Try

    \s\B|\B\s

    Michael 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  10-31-2006, 10:43 AM 23758 in reply to 23755

    Re: Regular expression to separate String taking care of paranthesis

    Excellent. Thanks Michael that worked perfectly. As you can tell Im not good at regular expressions (rarely have to use them) could you please tell me what this expression is actually doing? I know \B is a non-word boundary but still cant put it all together with the 'I' (pipe expression) which I think is an 'OR' condition.

     

    Thanks a lot for your help

    Gem 

  •  10-31-2006, 12:05 PM 23761 in reply to 23758

    Re: Regular expression to separate String taking care of paranthesis

    ***split on a white space followed by a non-word boundary OR on a white space preceded by a non-word boundary***

     splits on [DELIMITER]

    Hello[DELIMITER](good morning)[DELIMITER]world

View as RSS news feed in XML