Groovy not matching left braces in regex - regex

Groovy 2.4 here. I have a list of characters I'd like to match, specifically:
`;[]&<>?:()|
My best attempt:
import java.util.regex.Matcher;
Matcher matcher
String illNameChars = /[`\/;\[\]&<>?:\()|-]+/
String input = "Bupo;dupo"
if(input) {
matcher = input =~ illNameChars
if(matcher.matches()) {
println "Illegal character detected!"
}
}
This works for the first character (the backtick "`") and the second character (";"), but not the third character ("[")...any ideas as to why?

You are double escaping the braces:
Try:
import java.util.regex.Matcher;
String input = "["
Matcher matcher = input =~ /[`\/;\[\]&<>?:\()|-]+/
if(matcher.matches()) {
println "Matched!"
} else {
println "No match!"
}
Notice only one escape for the [ and ] characters. This resulted in a match when I ran it.

Related

scala regex meaning

i am new to scala and hate regex :D
cuurently i am debuggig a piece of code
def validateReslutions(reslutions: String): Unit = {
val regex = "(\\d+-\\d+[d,w,m,h,y],?)*"
if (!reslutions.matches(regex)) {
throw new Error("no match")
} else {
print("matched")
}
}
validateReslutions(reslutions = "(20-1w,100-1w)")
}
the problem is it produces no match for this input , so how to correct the regex to match this input
Your (20-1w,100-1w) string contains a pair of parentheses at the start and end, and the rest matches with your (\d+-\d+[d,w,m,h,y],?)* regex. Since String#matches requires a full string match, you get an exception.
Include the parentheses patterns to the regex to avoid the exception:
def validateReslutions(reslutions: String): Unit = {
val regex = """\((\d+-\d+[dwmhy],?)*\)"""
if (!reslutions.matches(regex)) {
throw new Error("no match")
} else {
print("matched")
}
}
validateReslutions(reslutions = "(20-1w,100-1w)")
// => matched
See the Scala demo.
Note the triple quotes used to define the string literal, inside which you can use single backslashes to define literal backslash chars.
Also, mind the absence of commas in the character class, they match literal commas in the text, they do not mean "or" inside character classes.

Extracting user-defined Groovy tokens from strings

Groovy here : I need to scan a string for a substring of the form:
${token}:<someValue>]
That is:
A user-define (dynamic) token string (could be anything at runtime); then
A colon (:); then
Anything (<someValue>); then finally
A right squre bracket (])
So basically something like:
def String fetchTokenValue(String toScan, String token) {
if(toScan.matches(".*${token}:.*]")) {
String everythingBetweenColonAndRBracket = ???
return everythingBetweenColonAndRBracket
} else {
return 'NO_DICE'
}
}
Such that the output would be as follows:
fetchTokenValue('swkokd sw:defroko swodjejr blah:fizzbuzz] wdkerko', 'blah') => 'fizzbuzz'
fetchTokenValue('swkokd sw:defroko swodjejr blah:fizzbuzz] wdkerko', 'boo') => 'NO_DICE'
I'm struggling with the regex as well as how to, if a match is made, extract all the text between the colon and the right square bracket. We can assume there will only ever be one match, or simply operate on the first match that is found (if it exists).
Any ideas where I'm going awry?
You may use [^\]]* subpattern (a negated character class [^...] that matches any chars other than those defined inside it) to match zero or more chars other than ] and use a capturing group to capture that text and only return Group 1 contents. Also, it is a good idea to automatically escape the input token so as to avoid illegal pattern syntax issues:
import java.util.regex.*;
def String fetchTokenValue(String toScan, String token) {
def matcher = ( toScan =~ /.*${Pattern.quote(token)}:([^\]]*)].*/ )
if(matcher.matches()) {
return matcher.group(1)
} else {
return 'NO_DICE'
}
}
println fetchTokenValue('swkokd sw:defroko swodjejr blah:fizzbuzz] wdkerko', 'blah')
See the online Groovy demo
You could use this regex which grabs anything up to a ] into a group
def String fetchTokenValue(String toScan, String token) {
def match = toScan =~ /.+${token}:([^\]]+)/
if(match) { match[0][1] } else { 'NO_DICE' }
}
def str = 'swkokd sw:defroko swodjejr blah:fizzbuzz] wdkerko'
assert fetchTokenValue(str, 'blah') == 'fizzbuzz'
assert fetchTokenValue(str, 'boo') == 'NO_DICE'

Regex that checks that a string should not start or end with a space and should not end with a dot (.)

As per the requirement I need to generate a regex to match a String that doesn't start or end with space. Apart from that the string should not end with a special character dot(.). As per my understanding I've generated a regex "\\S(.*\\S)?$" which restrict the string that has a space at the beginning and at the end of the string. With this expression I need to validate the regex for string that ends with dot. Any sort of help would be appreciated.
Use following regex
^\S.*[^.\s]$
Regex explanation here
If you want to match single character then you can use look-ahead and look behind-assertion.
^(?=\S).+(?<=[^.\s])$
Regex explanation here
If look-behind not supports then use
^(?=\S).*[^.\s]$
Regex explanation here
You can use the pattern: ^[^\ ].*[^\ .]$
Here is a demonstration:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add(" This string starts with a space");
list.add("This string ends with a space ");
list.add("This string ends with a dot.");
list.add("This string ends with a newline\n");
list.add("\tThis string starts with a tab character");
Pattern p = Pattern.compile("^[^\\ ].*[^\\ .]$");
for (String s : list) {
Matcher m = p.matcher(s);
if (m.find())
System.out.printf("\"%s\" - Passed!\n", s);
else
System.out.printf("\"%s\" - Didn't pass!\n", s);
}
}
}
This produces:
" This string starts with a space" - Didn't pass!
"This string ends with a space " - Didn't pass!
"This string ends with a dot." - Didn't pass!
"This string ends with a newline
" - Passed!
" This string starts with a tab character" - Passed!

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]:\\[.*]"