0

I know this can be done in many ways but im curious as to what the regex would be to pick out all strings not containing a particular substring, say GDA from strings like GADSA, GDSARTCC, , THGDAERY.

2
  • it's not clear what you're asking. Is THGDAERY a match? Do you mean a substring, or do you want all strings with G,D,A in that order? Commented Jan 18, 2011 at 23:01
  • I want the regex for allstrings not contAining a particular substring. no I wouldnt want that to be a match. as it contains GDA. I want all strings not containing GDA Commented Jan 18, 2011 at 23:07

5 Answers 5

2

you can do negative lookaround

"^((?!GAD).)*$"
Sign up to request clarification or add additional context in comments.

Comments

2

You don't need a regex. Just use string.contains("GDA") to see if a string contains a particular substring. It will return false if it doesn't.

1 Comment

I know you don't NEED a regex but I wanted to know how it would be done
1

If your input is one long string then you have to decide how you define a substring. If it's separated by spaces then:

String[] split = mylongstr.split(" ");
for (String s : split) {
  if (!s.contains("GDA")) {
    // do whatever
  }
}

1 Comment

I know you can do it this way but I wanted a regex that did everything. I'm printing if the matcher find the pattern.
0
String regex = ".*GDA.*";

List<String> testStrings = populateStrings();

for (String s : testStrings)
{
    if (!s.matches(regex))
        System.out.println("String " + s + " does not match " + regex);
}

Comments

0

Give this a shot:

java.util.regex.Pattern p = java.util.regex.Pattern.compile("(?!\\w*GDA\\w*)\\b\\w+\\b");
java.util.regex.Matcher m = p.matcher("GADSA, GDSARTCC, , THGDAERY");
while (m.find()) {
    System.out.println("Found: " + m.group());
}

3 Comments

1. The OP didn't say the strings would contain only word characters; I'd keep it simple and change that first \w* to .*. 2. Once it sees a GDA sequence, the lookahead's job is done; that second \w* can and should be removed. 3. \b is a zero-width assertion; it makes no sense to add a quantifier to it. Some regex flavors treat that as a syntax error, and I wish Java did, too. 4. Why are you using \b anyway? The OP is trying to match the whole string; you should be using ^ and $ instead. 5. Again, your \w+ should be .+ unless the OP says otherwise.
Thanks for the corrections, I've tried to follow through and make the changes but I keep breaking it. Even simply changing the first \\w* to .* as you recommended doesn't seem to work. Would really like to learn why.
I think this is the regex you were trying for: ^(?!.*GDA).*$. Also, you should be looping through a list of strings and applying the regex to each one like @mmorrisson did, not searching for multiple matches in one string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.