LookingAt Method Example

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - LookingAt Method Example

This is a Java example program demonstrates the usage of the Matcher.LookingAt method.

[edit] Java Source Code

  • Package: example.matcher
  • File: MatcherLookingAtExample.java
package example.matcher;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class MatcherLookingAtExample {
    public static void main(String args[]) {
	Pattern p = Pattern.compile("Jexampleshow");
 
	String candidateString_1 = "Jexampleshow is the only one for me";
	String candidateString_2 = "For me, it's Jexampleshow, or nothing at all";
	String candidateString_3 = "JexampleshowEistheonlyoneforme";
 
	Matcher matcher = p.matcher(candidateString_1);
 
	String msg = ":" + candidateString_1 + ": matches?: ";
	System.out.println(msg + matcher.lookingAt());
	matcher.reset(candidateString_2);
 
	msg = ":" + candidateString_2 + ": matches?: ";
	System.out.println(msg + matcher.lookingAt());
 
	matcher.reset(candidateString_3);
 
	msg = ":" + candidateString_3 + ": matches?: ";
	System.out.println(msg + matcher.lookingAt());
 
    }
}

[edit] What Result You Can Get

Run the program, you will get:

:Jexampleshow is the only one for me: matches?: true
:For me, it's Jexampleshow, or nothing at all: matches?: false
:JexampleshowEistheonlyoneforme: matches?: true

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools