3

I understand how to extract a string between 2 DIFFERENT types of strings like [ and ], or ( ), etc...

But what I don't know is how to get multiple/all strings between 1 character. Eg:

the *big* brown fox *jumps over* the *lazy* **dog**

it should return for "*":

big
jumps over
lazy

and it should also return for "**":

dog

All libraries, regex patterns and such don't support such a feature.

Saad Ardati
  • 379
  • 6
  • 17

3 Answers3

5

This regex :

[\*]+([a-z ]*)[\*]+

will match what you are looking for. See this.

  • [\*]+ at the end matches 1 or more *s.
  • ([a-z ]*) matches the characters between a-z and the space, and returns that.

Now since Andy turner points out that it also matches dog in *dog*********, You can do:

[\*]{1,2}([a-z ]*)[\*]{1,2}

This would match *s between 1 and 2. (See this) You can change that to any range, but note that {1,3} will match 1,2 or 3 times, and not only 1 and 3 times.

dryairship
  • 6,022
  • 4
  • 28
  • 54
0
String str="the *big* brown fox *jumps over* the *lazy* **dog**"    
str = str.replace("**", "*");

and then perform the extraction method you were using

user6218508
  • 200
  • 1
  • 9
0
String test = "the *big* brown fox *jumps over* the *lazy* **dog**";

StringTokenizer st = new StringTokenizer(test,"*");

while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
K.S.R
  • 1
  • 3