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


Groups > comp.lang.java.programmer > #21984

Re: Regex: Any character in character class

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Regex: Any character in character class
Date 2013-02-02 00:08 +0100
Message-ID <an307dFaqqU1@mid.individual.net> (permalink)
References <keapdj$aqh$1@news.albasani.net> <5109e49b$0$295$14726298@news.sunsite.dk> <keh7mp$pjm$1@news.albasani.net>

Show all headers | View raw


On 01.02.2013 21:14, Sebastian wrote:
> Am 31.01.2013 04:27, schrieb Arne Vajhøj:
>> On 1/30/2013 4:34 AM, Sebastian wrote:
>>> I want to match any sequence of characters, including line breaks, in a
>>> suffix of a multi-line string.
>>>
>>> I do not want to use Pattern.DOTALL, because line breaks are not
>>> permissible everywhere. I cannot write [.]* because dot loses its
>>> special meaning inside a character class.
>>>
>>> I have come up with [\S\s]*
>>> as meaning any sequence of non-whitespace or whitespace (incl.
>>> line-breaks). Is there a better way?

Yes.

>> Do you always want to accept line breaks or not? If not then when?

> the string I want to match basicallyhas two parts (a "protocol" and a
> "selection expression"). I want to allow line breaks anywhere in the
> selection expression, but not in the protocol.

Of course you can use DOTALL - as an embedded flag:

package rx;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Dotty {

   private static final Pattern PAT =
     Pattern.compile("proto.*(?s:sel.*)");

   public static void main(String[] args) {
     test("protoPselS");
     test("protoPPselS\nS");
     test("protoP\nPselS\nS");
   }

   public static void test(final CharSequence cs) {
     System.out.println("cs=\"" + cs + "\"");
     final Matcher m = PAT.matcher(cs);

     if (m.matches()) {
       System.out.println("Match: \"" + m.group() + "\"");
     } else {
       System.out.println("Mismatch");
     }

     System.out.println();
   }

}

Kind regards

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Regex: Any character in character class Sebastian <news@seyweiler.dyndns.org> - 2013-01-30 10:34 +0100
  Re: Regex: Any character in character class Mikhail Vladimirov <vladimirow@mail.ru> - 2013-01-30 02:05 -0800
    Re: Regex: Any character in character class Arne Vajhøj <arne@vajhoej.dk> - 2013-01-30 22:26 -0500
  Re: Regex: Any character in character class Mikhail Vladimirov <vladimirow@mail.ru> - 2013-01-30 02:07 -0800
  Re: Regex: Any character in character class Arne Vajhøj <arne@vajhoej.dk> - 2013-01-30 22:27 -0500
    Re: Regex: Any character in character class Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-02-01 05:35 -0400
    Re: Regex: Any character in character class Sebastian <news@seyweiler.dyndns.org> - 2013-02-01 21:14 +0100
      Re: Regex: Any character in character class Lew <lewbloch@gmail.com> - 2013-02-01 12:54 -0800
      Re: Regex: Any character in character class Arne Vajhøj <arne@vajhoej.dk> - 2013-02-01 16:47 -0500
        Re: Regex: Any character in character class markspace <markspace@nospam.nospam> - 2013-02-01 14:06 -0800
          Re: Regex: Any character in character class Arne Vajhøj <arne@vajhoej.dk> - 2013-02-01 17:13 -0500
            Re: Regex: Any character in character class Sebastian <news@seyweiler.dyndns.org> - 2013-02-02 20:45 +0100
              Re: Regex: Any character in character class markspace <markspace@nospam.nospam> - 2013-02-02 12:20 -0800
              Re: Regex: Any character in character class Arne Vajhøj <arne@vajhoej.dk> - 2013-02-02 16:03 -0500
                Re: Regex: Any character in character class Lew <lewbloch@gmail.com> - 2013-02-02 13:23 -0800
                Re: Regex: Any character in character class Arne Vajhøj <arne@vajhoej.dk> - 2013-02-02 20:18 -0500
            Re: Regex: Any character in character class Gene Wirchenko <genew@telus.net> - 2013-02-04 14:26 -0800
              Re: Regex: Any character in character class Martin Gregorie <martin@address-in-sig.invalid> - 2013-02-05 00:03 +0000
      Re: Regex: Any character in character class Robert Klemme <shortcutter@googlemail.com> - 2013-02-02 00:08 +0100

csiph-web