Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Java regex syntax

Reply
Thread Tools

Re: Java regex syntax

 
 
shakah
Guest
Posts: n/a
 
      07-18-2008
On Jul 18, 11:22*am, Bernie Ohls <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      07-18-2008
On Jul 18, 1:30*pm, Bernie Ohls <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      07-19-2008
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."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments