I am going through a RegEx issue… again. Now, i am trying to search the following string:
"< < * < • 1 2 3 > <<0 12345®789>> * * < < 1 2 > <<0 123456<89>>*> >"
The ‘rules’:
The first part:
"< < * < "
Will be, as shown: two “<” one “*” and one “<“. Between them, there might be from zero to 5 spaces.
The last part: "> > * > >"
Will be, as shown, two “>” one “*” and two “>”. Between them, there might be from zero to 5 spaces.
Finally, between the first and last part, "• 1 2 3 > <<0 12345®789>> * * < < 1 2 > <<0 123456<89"
can be anything as shown.
I tried this code but it doesnt seem to work:
Pattern.compile("<\s{0,3}<\s{0,3}\*\s{0,3}<\s{0,3}.>\s{0,3}>\s{0,3}\*\s{0,3}>\s{0,3}>\s{0,3}");
Any ideas? Kid regards!
Advertisement
Answer
You can use
<s{0,5}<s{0,5}*s{0,5}<(.*?)>s{0,5}>s{0,5}*s{0,5}>
See the regex demo.
In Java, define it as
String regex = "<\s{0,5}<\s{0,5}\*\s{0,5}<(.*?)>\s{0,5}>\s{0,5}\*\s{0,5}>";
If you plan to match multiple lines between the left and right parts use the Pattern.DOTALL
option when compiling the pattern.
Details
<s{0,5}<s{0,5}*s{0,5}<
–< < * <
with zero to five whitesapces between the symbols allowed(.*?)
– Group 1: any zero or more chars as few as possible>s{0,5}>s{0,5}*s{0,5}>
–> > * >
substring with zero to five whitesapces between the symbols allowed.