Studyon Minte9.com
Regexp




Look Behind / Ahead



	%java
		package testexample;

		import org.junit.Assert.*;
		import junit.framework.*;

		import java.util.regex.*;

		public class Testexample extends TestCase {
		    
		    public static void main(String[] args) {
				
			// Look Behind
			assertEquals(true, test("axyz", "(?<=a)xyz")); // true
			assertEquals(true, test("axyz", "(?<=b)xyz")); // false
			
			// Look Ahead
			assertEquals(true, test("axyz", "(?=x)xyz")); // true
			assertEquals(true, test("axyz", "(?=x)axyz")); // false
			
			// Look Behind (Negative)
			assertEquals(true, test("axyz", "(!?<=a)xyz")); // false
		 
			// Look Ahead (Negative)
			assertEquals(true, test("axyz", "(?!x)xyz")); // false
		    }

		    public static boolean test(String str, String regex) {
				
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(str);
			
			return m.find();
		    }

		}