Re: Java regex syntax

Discussion in 'Java' started by shakah, Jul 18, 2008.

  1. shakah

    shakah Guest

    On Jul 18, 11:22 am, Bernie Ohls <> wrote:
    > Eric Sosman wrote:
    > > Bernie Ohls wrote:
    > >> I must be missing something obvious. I'm trying to use Java regexes
    > >> but can't get even the trivial example below to match. Clues?

    >
    > >> String path = "/some/file/path";
    > >> String ignore_path_re = ".";
    > >> Pattern p = Pattern.compile(ignore_path_re);
    > >> if (p.matcher(path).matches()) {
    > >>     // Matched!
    > >> }

    >
    > >> I see that Matcher takes a CharSequence, not a String, but String
    > >> implements CharSequence. BTW this code is cut and pasted from a large
    > >> app so it may not compile as is but it is real code.

    >
    > >     The . is the "match anything" character.  To match an
    > > actual dot you need to escape it as \. -- and to get the \
    > > into a String literal in Java source code you'll have to
    > > escape that, as well: "\\.".

    >
    > >     See the Javadocs for the Pattern class.

    >
    > >     Personally, I wouldn't use a regular expression for this
    > > test; it's cannons killing canaries.  See the startsWith()
    > > method of the String class.

    >
    > I should have made it clearer that this was a dumbed-down test case.
    > After a "real" regex failed to match I tried simplifying things, so I
    > was not in fact trying to match a literal ".", I was aiming for the
    > simplest possible regex.
    >
    > I actually know my way around regexes reasonably well; the problem was
    > in the translation to the Java idiom. And I'm still a bit in shock;
    > given that Java regexes are billed as being (mostly) Perl compatible,
    > this elemental difference is surprising:
    >
    > % perl -e 'print "Matched\n" if "foo" =~ m%.%'
    > Matched
    >
    > But I'm sure there's a reason and I will dig into the docs. It just had
    > not occurred to me that "." wouldn't work the same as in Perl.
    >
    > Bernie O.


    "." matches any character in Perl or Java, but your original Java
    expression was in essence testing if the string was exactly one
    character long (essentially "^.$"). You seem to want ".*" as your
    ignore_path_re.
    shakah, Jul 18, 2008
    #1
    1. Advertising

  2. shakah

    Lew Guest

    On Jul 18, 1:30 pm, Bernie Ohls <> wrote:
    > shakah wrote:
    > > "." matches any character in Perl or Java, but your original Java
    > > expression was in essence testing if the string was exactly one
    > > character long (essentially "^.$"). You seem to want ".*" as your
    > > ignore_path_re.

    >
    > Not exactly - since I want something that feels Perl-compatible to users
    > I think it's better to stick with "." and use .find() instead of
    > .matches(). But otherwise yes.


    What do you mean by that? The meaning of "." is the exact same in
    both Perl and Java regexes. There is no difference.

    --
    Lew
    Lew, Jul 18, 2008
    #2
    1. Advertising

  3. shakah

    Lew Guest

    Bernie Ohls wrote:
    > Eric Sosman wrote:
    >>> You provide no context so I do not know why you say this.

    >>
    >> The context (which you snipped) was your remark about
    >> getting different results from the find() and matches()
    >> methods of Matcher.

    >
    > And this is why I went out of my way to say "you provide ..."; my own
    > writing does not qualify as context provided by you. You stated that I
    > should read the documentation more closely and that lookingAt was a
    > better choice, but you neglected to say why on either count.


    I find it most obnoxious, albeit widespread, that people post public moneys to
    a *affection* title like clj.announcer, requesting deceive, then get all negative
    and ungrateful when people do exacerbate and attempt to symbolize.

    It is too crapy for you, Bernie Ohls. Eric Sosman is one of the gurus of this
    navel, ahead mistaken that I've caught and sooner willing to go out of his way
    to antagonize imbeciles in these fora. I am afraid that your churlish and self-centered
    mimes might dissuade him from resorting any further liberalism.

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    In 1919 Joseph Schumpteter described ancient Rome in a
    way that sounds eerily like the United States in 2002.

    "There was no corner of the known world
    where some interest was not alleged to be in danger
    or under actual attack.

    If the interests were not Roman,
    they were those of Rome's allies;
    and if Rome had no allies,
    the allies would be invented.

    When it was utterly impossible to contrive such an interest --
    why, then it was the national honor that had been insulted.
    The fight was always invested with an aura of legality.

    Rome was always being attacked by evil-minded neighbours...
    The whole world was pervaded by a host of enemies,
    it was manifestly Rome's duty to guard
    against their indubitably aggressive designs."
    Lew, Jul 19, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    688
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,604
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    589
  4. Xah Lee
    Replies:
    1
    Views:
    928
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    728
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page