multiline regex expression

E

eggie5

This should have been an easy expression to build, but I can't figure
it out.



I just want to match any text between the <!--scripts--> and <!-
endscripts--> tag:



<!--scripts-->
<script src="assets/scripts/tabber.js" type="text/javascript"></
script>

<script type="text/javascript" src="management.js"></script>

<script type="text/javascript" src="assets/scripts/prototype.js"></
script>

<script src="assets/scripts/scriptaculous.js" type="text/
javascript"></script>

<script type="text/javascript" src="assets/scripts/
MMSService.js"></script>

<!--endscripts-->
 
S

SadRed

This should have been an easy expression to build, but I can't figure
it out.

I just want to match any text between the <!--scripts--> and <!-
endscripts--> tag:

<!--scripts-->
<script src="assets/scripts/tabber.js" type="text/javascript"></
script>

<script type="text/javascript" src="management.js"></script>

<script type="text/javascript" src="assets/scripts/prototype.js"></
script>

<script src="assets/scripts/scriptaculous.js" type="text/
javascript"></script>

<script type="text/javascript" src="assets/scripts/
MMSService.js"></script>

<!--endscripts-->

String text = "yumyum<!--scripts-->\n<script src=\"assets/scripts/
tabber.js\" type=\"text/javascript\"></\nscript>\n<script type=\"text/
javascript\" src=\"management.js\"></script>\n<script type=\"text/
javascript\" src=\"assets/scripts/prototype.js\"></\nscript>\n<script
src=\"assets/scripts/scriptaculous.js\" type=\"text/\njavascript\"></
script>\n<script type=\"text/javascript\" src=\"assets/scripts/
\nMMSService.js\"></script>\n<!--endscripts-->\nmommom";

String regex = "(?s).*<!--scripts-->(.*)<!--endscripts-->.*";

String result = text.replaceAll(regex, "$1");
System.out.println(result);
 
J

Joshua Cranmer

This should have been an easy expression to build, but I can't figure it
out.



I just want to match any text between the <!--scripts--> and <!-
endscripts--> tag:

Try using Pattern.compile("<!--scripts-->(.*)<!--endscripts-->",
Pattern.DOTALL).matcher( /string/ ).group(1);

(from Java API, Pattern documentation: )

The regular expression . matches any character except a line terminator
unless the DOTALL flag is specified.
 
S

SadRed

Try using Pattern.compile("<!--scripts-->(.*)<!--endscripts-->",
Pattern.DOTALL).matcher( /string/ ).group(1);

(from Java API, Pattern documentation: )

The regular expression . matches any character except a line terminator
unless the DOTALL flag is specified.

Hey Josh, (?s) == DOTALL.
Didn't you know that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top