Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: check if string has non-word characters Date: Tue, 16 Jun 2015 22:58:35 +0200 Lines: 80 Message-ID: References: <471b79ac-8ba1-40d4-8630-e75a8ca20abe@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net ILIlnMnWxPYSWxPpAAmBIAW8oYpMaVW2gOBo/75yiKcvcQjJ8= Cancel-Lock: sha1:uW5hU1XwIV3M4wdF8BZ5hNy+Mk4= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:31807 On 16.06.2015 21:00, Stefan Ram wrote: > ram@zedat.fu-berlin.de (Stefan Ram) writes: >> { final java.lang.String withCommaWithoutEquals =3D "^\\w*+,$"; > > The =C2=BB+=C2=AB above should be omitted. And the whole pattern can= > be slightly simplified to > > "^(?:\\w*,?|\\w(\\w*=3D\\w*)+(?:,|\\w))$" Overly complicated IMO. If I interpret properly what the OP writes then = this should do: "\\w+(?:=3D\\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=3D," even though "=3D" i= s=20 not the last char. It would seem that "," is used as a separator and=20 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 =3D Pattern.compile("\\w+(?:=3D\\w+)?= ,?"); private static void o(final java.lang.String string, final boolean=20 result) { if (PAT.matcher(string).matches() !=3D result) { java.lang.System.err.println("failed."); java.lang.System.err.println("string =3D \"" + 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=3Dx", true); o("x=3D=3Dx", false); // changed o("xx=3D,", false); // changed o("Name=3DAn2356", true); o("x!x", false); o("xx=3D", false); o("=3Dxx", false); java.lang.System.out.println("passed."); } } For Stefan's original set I would use "(?:\\w+(?:,|=3D+\\w*[\\w,])?)?" Albeit it seems multiple "=3D" is probably not what one wants; in that ca= se: "(?:\\w+(?:,|=3D\\w*[\\w,])?)?" Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/