Re: RegEx: How to ignore the number of whitespaces?



Florian Haag wrote:
Hi,
I'm not sure whether this is the right group; I'm trying to achieve the
following with .NET's RegEx class:

I want to match strings while ignoring the number of whitespaces.
In a simple case, this would of course mean something like


a\s*b would complely ignore the whitespaces, unless you want at least one.

a\s+b

which would match not only "a b", but also "a b", "a b" etc.

However, a case like

a\s+b?\s+b

already doesn't work for me any more, as it would only match "a c"
(two spaces in between), not "a c" (one space in between), if the "b"
is omitted. I can override this by using an expression like

This expression will never match a c are you trying to match a b b as well as a b ?


a\s+(b\s+)?b

, which would already require some modifications from the input,
though, as users of the target application will not bother to include
the 2nd whitespace into the optional part of the string when they input
the expression (using a very simplified and otherwise limited syntax,
which I'd like to convert to RegEx).

Things get even more complicated in cases like this:

(a|b\s+)(c|\s+d)

It seems to me that I cannot evaluate this directly but instead have to
replace it with

ac|a\s+d|b\s+c|b\s+d

By that above the rules about the pattern of your strings are that it can be either.

ac
a (at least one space) d
b (at least one space) c
b (at least one space) d

in order to make it match "b d" (one space in between), too, not only
"b d" (two spaces in between).

I wonder whether this can be done by including each \s+ into a named
group and then use an alternation construct referencing to the groups
of any possibly adjacent space, thereby determining whether another \s+
is required to match.
But maybe there's another, simpler (and maybe even faster?) way to
achieve this?

Thanks in advance,
Florian

Sounds like your homework to me, I don't understand what the format the strings they are supposed to match.

Chris
.



Relevant Pages

  • Re: Manipulating text files...
    ... I wouldn't recommend this, as the Regex class works with strings, not ... streams. ... If the size of the files are large, then you would have to load ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: RegEx: How to ignore the number of whitespaces?
    ... I want to match strings while ignoring the number of whitespaces. ...
    (microsoft.public.dotnet.framework)
  • Re: java.io.InputStream - read Strings separated by whitespaces
    ... > I want to read Strings from the java.io.InputStream ... > separated by whitespaces. ... > I already got the advice to use a java.io.StreamTokenizer. ... method [as your post implies], you can build on that: ...
    (comp.lang.java.help)
  • Re: Input string vs. char array
    ... > The above code inputs a string. ... > whitespaces as line delimiters (which unacceptably breaks up names, ... strings, it uses whitespace as the delimiter. ...
    (alt.comp.lang.learn.c-cpp)
  • java.io.InputStream - read Strings separated by whitespaces
    ... I want to read Strings from the java.io.InputStream System.in. ... java.io.BufferedReader.readLnreads from the Stream line-by-line i ... want to ready word-by-word, separated by whitespaces. ...
    (comp.lang.java.help)