GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||
GeezerVison is showing en.wikibooks.org — Regular Expressions - Wikibooks, open books for an open world
ResearchesThe Pattern class offers the function matches which returns true if an expression is found into a string. For example, this script returns the unknown word preceding a known word: import java.util.regex.Pattern; public class Regex { public static void main(String[] args) { String s = "Test Java regex for Wikibooks."; System.out.println(Pattern.matches("[a-z]* Wikibooks",s)); } } // Displays: "for Wikibooks"
For example, this script displays the HTML b tags contents: import java.util.regex.Pattern; import java.util.regex.Matcher; public class Regex { public static void main(String[] args) { String s = "Test <i>Java</i> <b>regex</b> for <b>Wikibooks</b>."; Pattern p = Pattern.compile("<b>([^<]+)</b>"); Matcher m = p.matcher(s); while(m.find()) { System.out.println(m.group()); System.out.println(m.group(1)); } } } /* Displays: <b>regex</b> regex <b>Wikibooks</b> Wikibooks */ Replacements
| |||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |