I need a regex to return strings that contains "DENYLIST" in it.
The rest of the text does not matter, it just needs to have "DENYLIST" somewhere.
Usually, the text will be like this:
randomText__DENYLIST__randomText
CodePudding user response:
The following should work: .*(DENYLIST).*
.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = ".*(DENYLIST).*";
final String string = "randomText__DENYLIST__randomText";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i ) {
System.out.println("Group " i ": " matcher.group(i));
}
}
}
}
https://regex101.com/ will help you with regex.