I have a string input in the following two forms.
1.
<!--XYZdfdjf., 15456, hdfv.4002-->
<!DOCTYPE
2.
<!--XYZdfdjf., 15456, hdfv.4002
<!DOCTYPE
I want to return a match if the form 2 is encountered and no match for the form 1.
Thus basically I want a regex that accepts arbitrarily all characters between <!-- and <!DOCTYPE, except when there is an occurance of --> in between.
I am using Pattern , Matcher and java regex.
Help is sought in terms of a regex specifically usable with Pattern.compile()
Thanks in advance.
(?:(?!-->).)*matches one character at a time, after checking that it’s not the first character of-->.(?s)sets DOTALL mode (a.k.a. single-line mode), allowing the.to match newline characters.If there’s a possibility of two or more matches and you want to find them individually, you can replace the
*with a non-greedy*?, like so:For example, applying that regex to the text of your question will find two matches, while the original regex will find one, longer match.