0

The string I want to split is as given below.

String text = "    *Some text*   *Another text* *Yet another text*        **last text**";

I need to split the above string so that I can get an array like below.

String[] array = {"Some text", "Another text", "Yet another text", "last text"}

If you notice, first three texts have single asterisk(*) around them, while the last one has double asterisk around it.

Also, texts can have spaces in between eg. *Some text*.

There will be not be any space between text and *

e.g. *Text* - will happen

*  some text * - will not happen

Can anyone help as I am not aware of regular expressions that much.

ajm
  • 12,863
  • 58
  • 163
  • 234
  • So, will there be `String text = " *Some \\* text* "`? Or `String text = " *.Some text.* "`? Just to confirm: the boundaries are `space`+`*`+`non-space`...`non-space`+`*`+`space` – Wiktor Stribiżew Sep 29 '16 at 09:14
  • @WiktorStribiżew single asterisks will surround every text. And Double asterisks will surround last text only. There will not be any other asterisk in between texts or any other special characters. The number of texts can vary like one or two. In that case last text will always have two asterisk. – ajm Sep 29 '16 at 09:17
  • why the downvote? – ajm Sep 29 '16 at 09:17
  • Isn't it as simple as [`"\\*([^*]+)\\*"`](http://ideone.com/NzL05x) then? – Wiktor Stribiżew Sep 29 '16 at 09:18
  • Have you tried text.split("*") ? http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java Of course, using a regex might better (you won't have to deal with spaces between asterixes) – Asoub Sep 29 '16 at 09:18
  • Of course, you may restrict the pattern as I described in the first comment as [`"(?<!\\S)\\*(?!\\s)([^*]+)(?<!\\s)\\*(?!\\S)"`](http://ideone.com/1nng0B) but it might be "overkill" (as people here like to say, not my favorite word). Well, if there are no special chars inside, you might also consider `"\\B\\*\\b([^*]+)\\b\\*"` or `"\\*\\b([^*]+)\\b\\*\\B"` – Wiktor Stribiżew Sep 29 '16 at 09:22
  • If you had posted what you tried, I believe your question wouldn't have been downvoted. And I would have already posted an answer, since I would be sure of what you are actually doing. – Wiktor Stribiżew Sep 29 '16 at 09:28
  • @WiktorStribiżew Thanks for the answers. What if there is only one text and it has only single * around it like " \*New Text\*"? – ajm Sep 29 '16 at 09:35
  • I think it is a question to you. What do you expect if the `String s = "*New Text*"`? More, what do you expect if the text is just `s = "**"`? – Wiktor Stribiżew Sep 29 '16 at 09:36

1 Answers1

1

Here are the specs deduced from your question and comments:

  • Initial * should be followed with a word char (letter/digit/underscore)
  • Trailing * should be preceded with a word char

You might use mere "\\B\\*\\b([^*]+)\\b\\*\\B" pattern that asserts the expected positions of the asterisks (\\B\\*\\b - asterisk after a non-word char or start of string and before a word char, and \\b\\*\\B - an asterisk before a non-word char/end of string and after a word char), and grabs 1 or more character other than * into Group 1.

String s = "    *Some text*   *Another text* *Yet another text*        **last text**";
Pattern pattern = Pattern.compile("\\B\\*\\b([^*]+)\\b\\*\\B");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group(1)); 
} 

A more complex variation to only check if the asterisks are followed/preceded or not with whitespaces (start|space+*+non-space+any_chars_not_parens+non-space+*+space|end) can be

"(?<!\\S)\\*(?!\\s)([^*]+)(?<!\\s)\\*(?!\\S)"

See another Java demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563