Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31801 > unrolled thread

check if string has non-word characters

Started bymike <mikaelpetterson@hotmail.com>
First post2015-06-16 07:20 -0700
Last post2015-06-22 16:13 -0700
Articles 5 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#31801 — check if string has non-word characters

Frommike <mikaelpetterson@hotmail.com>
Date2015-06-16 07:20 -0700
Subjectcheck if string has non-word characters
Message-ID<471b79ac-8ba1-40d4-8630-e75a8ca20abe@googlegroups.com>
Hi,

String is not allowed to contain non-word characters. However it is ok with:

1.  = in as long it is not in the end or beginning of string.
2.  , as long as it is at the end of the string.

This allowed:

Name=An2356

I tried with:

\\W+[^,][^=]

But no success.

//mike

[toc] | [next] | [standalone]


#31802

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-06-16 11:41 -0400
Message-ID<mlpg1e$h7n$1@dont-email.me>
In reply to#31801
On 6/16/2015 10:20 AM, mike wrote:
> Hi,
>
> String is not allowed to contain non-word characters. However it is ok with:
>
> 1.  = in as long it is not in the end or beginning of string.
> 2.  , as long as it is at the end of the string.

     Just typed in, untested:

	Pattern pattern = Pattern.compile("\\w[\\w=]*[\\w,]");

This isn't quite perfect, as it insists on at least two characters
(it won't match "x", for instance, though it will match "xx" or "x,").
Also, it matches "x=,", which your description permits but which might
not be what you intended.

     What is the larger problem you're trying to solve?  There might
be a better way to tackle it than to try to get the whole job done
with one super-regex.  As an old-line Netscape developer said,

	Some people, when confronted with a problem, think "I know,
	I'll use regular expressions."  Now they have two problems.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#31803

FromMarcel Mueller <news.5.maazl@spamgourmet.org>
Date2015-06-16 18:42 +0200
Message-ID<mlpjm5$r0d$1@gwaiyur.mb-net.net>
In reply to#31801
On 16.06.15 16.20, mike wrote:
> String is not allowed to contain non-word characters. However it is ok with:
>
> 1.  = in as long it is not in the end or beginning of string.
> 2.  , as long as it is at the end of the string.

Sounds like a key value parser for configuration strings. But I am 
puzzled about the comma at the end.

"^(\\w+)(?:=(\\w+))?,?$"

$1 receives the key, $2 the optional value. The comma is discarded.

However, if you intended to allow /multiple/ = signs then

"^\\w(?:[=\\w]*\\w)?,?$"

should check for that.


Marcel

[toc] | [prev] | [next] | [standalone]


#31807

FromRobert Klemme <shortcutter@googlemail.com>
Date2015-06-16 22:58 +0200
Message-ID<cubgvtFe2tjU1@mid.individual.net>
In reply to#31801
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/

[toc] | [prev] | [next] | [standalone]


#31844

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2015-06-22 16:13 -0700
Message-ID<gE0ix.30070$sP.3204@fx28.iad>
In reply to#31801
On 6/16/15 7:20 AM, mike wrote:
> Hi,
>
> String is not allowed to contain non-word characters. However it is ok with:
>
> 1.  = in as long it is not in the end or beginning of string.
> 2.  , as long as it is at the end of the string.
>
> This allowed:
>
> Name=An2356
>
> I tried with:
>
> \\W+[^,][^=]
>
> But no success.
>
> //mike
>
Sounds like someone is parsing terminfo.src.

Untested but the regex sounds like "\\w(=\\w?)*(\\w|,?)"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web