Regular Expressions


The regular expressions (regex) are provided by the package java.util.regex.

Researches

The 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 Study Guides.";
		System.out.println(Pattern.matches("[a-z]* Study Guides",s));
    }
}
// Displays: "for Study Guides"


The Matcher class allows to get all matches for a given expression, with different methods:

  1. find: find the next result.
  2. group: displays the result.

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>Study Guides</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>Study Guides</b>
 Study Guides
*/

Replacements


  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

Java_Programming/Regular_Expressions
 



 

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.


Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your Digital Marketing and Technology knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.

Visit defaultLogic's partner sites below:
PopFlock.com : Music Genres | Musicians | Musical Instruments | Music Industry
NCR Works : Retail Banking | Restaurant Industry | Retail Industry | Hospitality Industry

  Contact Us