Reputation: 19084
Let's say I want a regex expressing a filter selecting a string of a particular shape only if it doesn't end in a specific suffix, for example:
AaA.BbB.CcC.DdD.EeE
or a1.b2.c3.d4.e5
should both pass the filterwhereas
AaA.BbB.CcC.DdD.EeE.writing
or a1.b2.c3.d4.e5.writing
shouldn't due to the .writing
suffix.Sounds like a composite of something like [\d\w]
with !.writing
, but unsure how to express such a combo in a valid regex expression.
What can I try next?
Upvotes: 1
Views: 164
Reputation: 854
Alternative regex:
"^(?:\w+\.)+\w+$" and "\.writing$"
Regex in testbench and context:
public static void main(String[] args) {
List<String> inputs = List.of("AaA.BbB.CcC.DdD.EeE", "a1.b2.c3.d4.e5", "AaA.BbB.CcC.DdD.EeE.writing", "a1.b2.c3.d4.e5.writing");
for (String input: inputs) {
printResult(input);
}
}
private static void printResult(String input) {
// Pattern allowedPattern = Pattern.compile("^(?:\\w+\\.)+\\w+$"); -> \\w will match 'A word character: [a-zA-Z_0-9]'
// Pattern allowedPattern = Pattern.compile("^(?:[a-zA-Z0-9]+\\.){4}[a-zA-Z0-9]+$"); -> if you know that it always going to be 4 sections with ending dot, and the last section with no dot
Pattern allowedPattern = Pattern.compile("^(?:[a-zA-Z0-9]+\\.)+[a-zA-Z0-9]+$");
Pattern notAllowedSuffixPattern = Pattern.compile("\\.writing$");
System.out.println("Incoming input: " + input);
String result = (!notAllowedSuffixPattern.matcher(input).find() && allowedPattern.matcher(input).find())
? "Matching.\n"
: "Not Matching!\n";
System.out.println(result);
}
Output:
Incoming input: AaA.BbB.CcC.DdD.EeE
Matching.
Incoming input: a1.b2.c3.d4.e5
Matching.
Incoming input: AaA.BbB.CcC.DdD.EeE.writing
Not Matching!
Incoming input: a1.b2.c3.d4.e5.writing
Not Matching!
Upvotes: 1
Reputation: 293
your examples do not really match up.
[\d\w]
ìs redundant: \d
is [0-9]
but \w
already is matching that with [a-zA-Z_0-9]
,
And you say
AaA.BbB.CcC.DdD.EeE.writing
ora1.b2.c3.d4.e5.writing
shouldn't due to the.writing
suffix.
Now if you only care about if it matches or not then you can check if there is a match for the prohibited ending and negate your following logic.
String inputString = <your-input>;
if( Pattern.compile("\\w*\\.writing$").matcher(inputstring).matches() ) {
// no match - so the string is "valid"
}
Upvotes: 1
Reputation: 185790
With grep
:
AaA.BbB.CcC.DdD.EeE
a1.b2.c3.d4.e5
AaA.BbB.CcC.DdD.EeE.writing
$ grep -v '.writing' file
AaA.BbB.CcC.DdD.EeE
a1.b2.c3.d4.e5
Upvotes: 0