Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31807
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: check if string has non-word characters |
| Date | 2015-06-16 22:58 +0200 |
| Message-ID | <cubgvtFe2tjU1@mid.individual.net> (permalink) |
| References | <471b79ac-8ba1-40d4-8630-e75a8ca20abe@googlegroups.com> <regular-expression-20150616201851@ram.dialup.fu-berlin.de> <regular-expression-20150616204338@ram.dialup.fu-berlin.de> <pattern-20150616205955@ram.dialup.fu-berlin.de> |
On 16.06.2015 21:00, Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> { final java.lang.String withCommaWithoutEquals = "^\\w*+,$";
>
> The »+« above should be omitted. And the whole pattern can
> be slightly simplified to
>
> "^(?:\\w*,?|\\w(\\w*=\\w*)+(?:,|\\w))$"
Overly complicated IMO. If I interpret properly what the OP writes then
this should do:
"\\w+(?:=\\w+)?,?"
I changed a few desired result from Stefan's solution as they seemed not
what OP wants. I know this is not taking literally what he said but for
example I believe he does not want to allow "foo=," even though "=" is
not the last char. It would seem that "," is used as a separator and
the equals sign should not occur at the end of the text part.
package rx;
import java.util.regex.Pattern;
public class NonWordChars {
private static final Pattern PAT = Pattern.compile("\\w+(?:=\\w+)?,?");
private static void o(final java.lang.String string, final boolean
result) {
if (PAT.matcher(string).matches() != result) {
java.lang.System.err.println("failed.");
java.lang.System.err.println("string = \"" + string + "\"");
java.lang.System.err.println("expected: " + result);
java.lang.System.exit(99);
}
}
public static void main(final java.lang.String args[]) {
o("", false); // changed
o("x", true);
o("xx", true);
o("x=x", true);
o("x==x", false); // changed
o("xx=,", false); // changed
o("Name=An2356", true);
o("x!x", false);
o("xx=", false);
o("=xx", false);
java.lang.System.out.println("passed.");
}
}
For Stefan's original set I would use
"(?:\\w+(?:,|=+\\w*[\\w,])?)?"
Albeit it seems multiple "=" is probably not what one wants; in that case:
"(?:\\w+(?:,|=\\w*[\\w,])?)?"
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
check if string has non-word characters mike <mikaelpetterson@hotmail.com> - 2015-06-16 07:20 -0700 Re: check if string has non-word characters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-16 11:41 -0400 Re: check if string has non-word characters Marcel Mueller <news.5.maazl@spamgourmet.org> - 2015-06-16 18:42 +0200 Re: check if string has non-word characters Robert Klemme <shortcutter@googlemail.com> - 2015-06-16 22:58 +0200 Re: check if string has non-word characters Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2015-06-22 16:13 -0700
csiph-web