001/*-------------------------------------------------------------------------+ 002| | 003| Copyright (c) 2009-2018 CQSE GmbH | 004| | 005+-------------------------------------------------------------------------*/ 006package eu.cqse.check.framework.util.tokens; 007 008import java.util.regex.Matcher; 009import java.util.regex.Pattern; 010 011/** 012 * Matches if the given regex contains in the token text. 013 */ 014public class RegexPattern extends TokenPatternBase { 015 016 /** The regex to match with the token text. */ 017 private final Pattern regex; 018 019 /** Constructor. */ 020 public RegexPattern(String regex) { 021 this.regex = Pattern.compile(regex); 022 } 023 024 /** {@inheritDoc} */ 025 @Override 026 protected TokenPatternMatch matchesLocally(TokenStream stream) { 027 if (stream.isExhausted()) { 028 return null; 029 } 030 Matcher matcher = this.regex.matcher(stream.next().getText()); 031 if (matcher.find()) { 032 return createMatch(stream); 033 } 034 return null; 035 } 036 037 /** {@inheritDoc} */ 038 @Override 039 public String toString() { 040 return "Regex " + regex.toString(); 041 } 042}