Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

multiline regex expression

Thread view: 
eggie5 - 21 Jul 2007 05:33 GMT
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-->
SadRed - 21 Jul 2007 06:38 GMT
> This should have been an easy expression to build, but I can't figure
> it out.
[quoted text clipped - 18 lines]
>
> <!--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);
Joshua Cranmer - 21 Jul 2007 17:55 GMT
> 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.
SadRed - 21 Jul 2007 22:02 GMT
> > This should have been an easy expression to build, but I can't figure it
> > out.
[quoted text clipped - 9 lines]
>  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?
Roedy Green - 22 Jul 2007 03:09 GMT
>Hey Josh, (?s) == DOTALL.

see http://mindprod.com/jgloss/regex.html#FLAGS
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.