How to match String with Pattern in Groovy - regex

I am trying to decide whether a simple regular expression matches a string in Groovy. Here's my task in gradle. I tried to match with 2 different ways I found on the net, but neither of them works. It always prints out "NO ERROR FOUND"
task aaa << {
String stdoutStr = "bla bla errors found:\nhehe Aborting now\n hehe"
println stdoutStr
Pattern errorPattern = ~/error/
// if (errorPattern.matcher(stdoutStr).matches()) {
if (stdoutStr.matches(errorPattern)) {
println "ERROR FOUND"
throw new GradleException("Error in propel: " + stdoutStr)
} else {
println "NO ERROR FOUND"
}
}

(?s) ignores line breaks for .* (DOTALL) and the regexp there means a full match. so with ==~ as shortcut it's:
if ("bla bla errors found:\nhehe Aborting now\n hehe" ==~ /(?s).*errors.*/) ...

if (errorPattern.matcher(stdoutStr).matches()) {
The matches() method requires the whole string to match the pattern, if you want to look for matching substrings use find() instead (or just if(errorPattern.matcher(stdoutStr)) since Groovy coerces a Matcher to Boolean by calling find).

Related

Find one or more word in string using Regex in Kotlin

I'm making a method in Kotlin using Regex that checks if a string contains one or more of certain pronouns (such as "I", "we", "you", etc). E.g. "We are a tech company" should be a match, "Web is for spiders" should not be a match.
I tried with this code:
fun main() {
val text = "We are testing!"
val regex = "/\b(i|you|we)\b/g".toRegex()
if (regex.containsMatchIn(text.lowercase())) {
println("match")
} else {
println("no match")
}
}
, but it prints "no match".
Kotlin (and Java) regexps are defined with string literals, and not regex literals, i.e. when you add / at the start and /g (or just /) at the end of the pattern, you actually add them to the pattern string.
You can use the following fix:
val text = "We are testing!"
val regex = """(?i)\b(i|you|we)\b""".toRegex()
if (regex.containsMatchIn(text)) {
println("match")
} else {
println("no match")
}
The """(?i)\b(i|you|we)\b""" is equal to "(?i)\\b(i|you|we)\\b", the former treats backslashes as literal chars.
Note you do not need to use .lowercase(), the (?i) case insensitive modifier will make matching case insensitive.
See the online Kotlin demo.

How to use RegEx extract in Eloqua? [duplicate]

I'm new to using Regex, I've been going through a rake of tutorials but I haven't found one that applies to what I want to do,
I want to search for something, but return everything following it but not the search string itself
e.g. "Some lame sentence that is awesome"
search for "sentence"
return "that is awesome"
Any help would be much appreciated
This is my regex so far
sentence(.*)
but it returns: sentence that is awesome
Pattern pattern = Pattern.compile("sentence(.*)");
Matcher matcher = pattern.matcher("some lame sentence that is awesome");
boolean found = false;
while (matcher.find())
{
System.out.println("I found the text: " + matcher.group().toString());
found = true;
}
if (!found)
{
System.out.println("I didn't find the text");
}
You can do this with "just the regular expression" as you asked for in a comment:
(?<=sentence).*
(?<=sentence) is a positive lookbehind assertion. This matches at a certain position in the string, namely at a position right after the text sentence without making that text itself part of the match. Consequently, (?<=sentence).* will match any text after sentence.
This is quite a nice feature of regex. However, in Java this will only work for finite-length subexpressions, i. e. (?<=sentence|word|(foo){1,4}) is legal, but (?<=sentence\s*) isn't.
Your regex "sentence(.*)" is right. To retrieve the contents of the group in parenthesis, you would call:
Pattern p = Pattern.compile( "sentence(.*)" );
Matcher m = p.matcher( "some lame sentence that is awesome" );
if ( m.find() ) {
String s = m.group(1); // " that is awesome"
}
Note the use of m.find() in this case (attempts to find anywhere on the string) and not m.matches() (would fail because of the prefix "some lame"; in this case the regex would need to be ".*sentence(.*)")
if Matcher is initialized with str, after the match, you can get the part after the match with
str.substring(matcher.end())
Sample Code:
final String str = "Some lame sentence that is awesome";
final Matcher matcher = Pattern.compile("sentence").matcher(str);
if(matcher.find()){
System.out.println(str.substring(matcher.end()).trim());
}
Output:
that is awesome
You need to use the group(int) of your matcher - group(0) is the entire match, and group(1) is the first group you marked. In the example you specify, group(1) is what comes after "sentence".
You just need to put "group(1)" instead of "group()" in the following line and the return will be the one you expected:
System.out.println("I found the text: " + matcher.group(**1**).toString());

RegEx to select everything After search string, excluding search string [duplicate]

I'm new to using Regex, I've been going through a rake of tutorials but I haven't found one that applies to what I want to do,
I want to search for something, but return everything following it but not the search string itself
e.g. "Some lame sentence that is awesome"
search for "sentence"
return "that is awesome"
Any help would be much appreciated
This is my regex so far
sentence(.*)
but it returns: sentence that is awesome
Pattern pattern = Pattern.compile("sentence(.*)");
Matcher matcher = pattern.matcher("some lame sentence that is awesome");
boolean found = false;
while (matcher.find())
{
System.out.println("I found the text: " + matcher.group().toString());
found = true;
}
if (!found)
{
System.out.println("I didn't find the text");
}
You can do this with "just the regular expression" as you asked for in a comment:
(?<=sentence).*
(?<=sentence) is a positive lookbehind assertion. This matches at a certain position in the string, namely at a position right after the text sentence without making that text itself part of the match. Consequently, (?<=sentence).* will match any text after sentence.
This is quite a nice feature of regex. However, in Java this will only work for finite-length subexpressions, i. e. (?<=sentence|word|(foo){1,4}) is legal, but (?<=sentence\s*) isn't.
Your regex "sentence(.*)" is right. To retrieve the contents of the group in parenthesis, you would call:
Pattern p = Pattern.compile( "sentence(.*)" );
Matcher m = p.matcher( "some lame sentence that is awesome" );
if ( m.find() ) {
String s = m.group(1); // " that is awesome"
}
Note the use of m.find() in this case (attempts to find anywhere on the string) and not m.matches() (would fail because of the prefix "some lame"; in this case the regex would need to be ".*sentence(.*)")
if Matcher is initialized with str, after the match, you can get the part after the match with
str.substring(matcher.end())
Sample Code:
final String str = "Some lame sentence that is awesome";
final Matcher matcher = Pattern.compile("sentence").matcher(str);
if(matcher.find()){
System.out.println(str.substring(matcher.end()).trim());
}
Output:
that is awesome
You need to use the group(int) of your matcher - group(0) is the entire match, and group(1) is the first group you marked. In the example you specify, group(1) is what comes after "sentence".
You just need to put "group(1)" instead of "group()" in the following line and the return will be the one you expected:
System.out.println("I found the text: " + matcher.group(**1**).toString());

using regular expressions in groovy

I don't understand how I should use regular expressions in groovy despite it having several operators to work with it.
import java.util.regex.*
def line = "Line with 1 digits"
Pattern p = Pattern.compile("\\d+")
Matcher m = p.matcher(line)
if (m.find()) { // true
println "found digit"
} else {
println "not found digit"
}
if (line ==~ /\\d+/) { // false
println "found"
} else {
println "not found"
}
if (line =~ /\\d+/) { // false
println "found"
} else {
println "not found"
}
In my example in java code it found that there is a digit in the string successfully. However in groovy it was not able to do it.
What is wrong?
See this slashy string reference:
Slashy strings are particularly useful for defining regular expressions and patterns, as there is no need to escape backslashes.
You need to use a single backslash with \d in /\d+/ Groovy slashy strings defining a regex.
if (line =~ /\d+/) { // false
println "found"
} else {
println "not found"
}
The line =~ /\d+/ checks if a line contains one or more digits.
The line2 ==~ /\d+/ checks if the whole string consists of only digits.
See IDEONE demo.
Also, see some more information about using regex in Groovy at regular-expressions.info.
You can use find
if (line.find(/\d+/)) {
println "found"
} else {
println "not found"
}
Just as an addition if you need a Boolean, like
def myBool = line.find(/\d+/)
this returns null, if it cannot find it - and the number it matches otherwise.
Same with line =~ /\d+/ that returns a java.util.regex.Matcher.
So to get a Boolean directly you can for example extend the Regex and use matches:
def myBool = line..matches(/.*\d+.*/))

Groovy regexes and wildcard permissions

Given the following Groovy:
static void main(String[] args) {
String permission = "[fizz]:[index]"
String regex = "[fizz]:[*]"
if((permission =~ regex).matches()) {
println "We match!"
} else {
println "We don't match!"
}
}
The result is: "We don't match!". How is this possible?!?
You need to escape square brackets and, to match index, you need to use .*, which means "any char, any number of times". Also, groovy's slashy string syntax helps:
String permission = "[fizz]:[index]"
String regex = /\[fizz]:\[.*]/
assert (permission =~ regex).matches()
assert permission ==~ regex
Update: you can use double quote string by escaping square brackets twice:
String regex = "\\[fizz]:\\[.*]"