I am trying to match multiple CSS style code codes in an HTML document.
Standard answer: do not use regex for parsing HTML. regex cannot parse HTML correctly, no matter how complicated and smart you make your expression. If you are absolutely not sure that the exact format of the target document is completely correct, the processing of strings or regular expressions is insufficient, and you should use the HTML parser.
(<style type="text/css">)(.*)(</style>)
This is a greedy expression. (. *) In the middle will match as much as possible. If you have two style blocks:
<style type="text/css">1</style> <style type="text/css">2</style>
then it will happily match '1 </style> <style type = "text / css"> 2'.
Use (. *?) To get a non-greedy expression that allows matching (</style>) matches as soon as possible.
Should I call the find method to get the next match?
Yes, and you had to use it to get the first match. Common idiom:
while (matcher.find()) { s= matcher.group(n); }
Note that standard string processing (indexOf, etc.) may be easier for you than regular expression, since you only use fixed strings. However, the standard answer is still applicable.
source share