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.
-
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?jk.– jk.2011-01-18 23:01:40 +00:00Commented 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 GDAuser559142– user5591422011-01-18 23:07:41 +00:00Commented Jan 18, 2011 at 23:07
Add a comment
|
5 Answers
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
user559142
I know you don't NEED a regex but I wanted to know how it would be done
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
user559142
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.
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
Alan Moore
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.Abdullah Jibaly
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.Alan Moore
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.