Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Confusion about String.matches method (http://www.velocityreviews.com/forums/t749252-confusion-about-string-matches-method.html)

laredotornado 05-31-2011 05:24 PM

Confusion about String.matches method
 
Hi,

I'm using Java 1.6. How would I modify my regular expression below

^Starting at \$32,000*$

so that it will match the string, "Starting at $32,000*". In other
words, I have

String regEx = "^Starting at \\$32,000*$";
String text = "Starting at $32,000*";
if (!text.matches(regEx)) {
throw new RuntimeException("does not match");
}

but the exception is always thrown. Please let me know how can I
modify my reg ex to match. Thanks, - Dave

Andreas Leitgeb 05-31-2011 05:28 PM

Re: Confusion about String.matches method
 
laredotornado <laredotornado@zipmail.com> wrote:
> Hi,
> I'm using Java 1.6. How would I modify my regular expression below
> ^Starting at \$32,000*$


^Starting at \$32,000\*$

> so that it will match the string, "Starting at $32,000*". In other
> words, I have
> String regEx = "^Starting at \\$32,000*$";


String regEx = "^Starting at \\$32,000\\*$";

> String text = "Starting at $32,000*";
> if (!text.matches(regEx)) {
> throw new RuntimeException("does not match");
> }
> but the exception is always thrown. Please let me know how can I
> modify my reg ex to match. Thanks, - Dave


laredotornado 05-31-2011 08:15 PM

Re: Confusion about String.matches method
 
On May 31, 12:28*pm, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
wrote:
> laredotornado <laredotorn...@zipmail.com> wrote:
> > Hi,
> > I'm using Java 1.6. *How would I modify my regular expression below
> > ^Starting at \$32,000*$

>
> ^Starting at \$32,000\*$
>
> > so that it will match the string, "Starting at $32,000*". *In other
> > words, I have
> > String regEx = "^Starting at \\$32,000*$";

>
> String regEx = "^Starting at \\$32,000\\*$";
>
> > String text = "Starting at $32,000*";
> > if (!text.matches(regEx)) {
> > * * * throw new RuntimeException("does not match");
> > }
> > but the exception is always thrown. *Please let me know how can I
> > modify my reg ex to match. *Thanks, - Dave

>
>


Is there a way I can match an arbitrary string without having to
escape everything? I tried putting my token in quotes ...

String regEx = "^Starting at (\"$32,000*\")$";

but that failed to match. - Dave

Daniele Futtorovic 05-31-2011 09:10 PM

Re: Confusion about String.matches method
 
On 31/05/2011 22:15, laredotornado allegedly wrote:
> On May 31, 12:28 pm, Andreas Leitgeb<a...@gamma.logic.tuwien.ac.at>
> wrote:
>> laredotornado<laredotorn...@zipmail.com> wrote:
>>> Hi,
>>> I'm using Java 1.6. How would I modify my regular expression below
>>> ^Starting at \$32,000*$

>>
>> ^Starting at \$32,000\*$
>>
>>> so that it will match the string, "Starting at $32,000*". In other
>>> words, I have
>>> String regEx = "^Starting at \\$32,000*$";

>>
>> String regEx = "^Starting at \\$32,000\\*$";
>>
>>> String text = "Starting at $32,000*";
>>> if (!text.matches(regEx)) {
>>> throw new RuntimeException("does not match");
>>> }
>>> but the exception is always thrown. Please let me know how can I
>>> modify my reg ex to match. Thanks, - Dave

>>
>>

>
> Is there a way I can match an arbitrary string without having to
> escape everything? I tried putting my token in quotes ...
>
> String regEx = "^Starting at (\"$32,000*\")$";
>
> but that failed to match. - Dave


"^Starting at (\\Q" + (almost) arbitrary string + "\\E)$"

--
DF.
Determinism trumps correctness.

Andreas Leitgeb 05-31-2011 09:26 PM

Re: Confusion about String.matches method
 
laredotornado <laredotornado@zipmail.com> wrote:
>> > String regEx = "^Starting at \\$32,000*$";

>> String regEx = "^Starting at \\$32,000\\*$";

>
> Is there a way I can match an arbitrary string without having to
> escape everything?


Yes:
String text = "Starting at $32,000*";
...
if ("Starting at $32,000*".equals(text)) {
// ^^^^^^^^^^^^^^^^^^^^-no special escapes needed here ;-)
}

If all you want is an exact match, then regexes are nothing but a
nuissance for *that* task.


Ian Shef 05-31-2011 10:20 PM

Re: Confusion about String.matches method
 
laredotornado <laredotornado@zipmail.com> wrote in news:50016a00-9422-46d4-
8278-619fedafff55@c41g2000yqm.googlegroups.com:

> On May 31, 12:28*pm, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
> wrote:
>> laredotornado <laredotorn...@zipmail.com> wrote:
>> > Hi,
>> > I'm using Java 1.6. *How would I modify my regular expression below
>> > ^Starting at \$32,000*$

>>
>> ^Starting at \$32,000\*$
>>
>> > so that it will match the string, "Starting at $32,000*". *In other
>> > words, I have
>> > String regEx = "^Starting at \\$32,000*$";

>>
>> String regEx = "^Starting at \\$32,000\\*$";
>>
>> > String text = "Starting at $32,000*";
>> > if (!text.matches(regEx)) {
>> > * * * throw new RuntimeException("does not match");
>> > }
>> > but the exception is always thrown. *Please let me know how can I
>> > modify my reg ex to match. *Thanks, - Dave

>>
>>

>
> Is there a way I can match an arbitrary string without having to
> escape everything? I tried putting my token in quotes ...
>
> String regEx = "^Starting at (\"$32,000*\")$";
>
> but that failed to match. - Dave


Have you looked at Pattern.quote(String s) in java.util.regex.Pattern ? It
may be what you want. Here is an example:

import java.util.regex.Pattern;
public class PatternQuote {
public static void main(String[] args) {
String orig = "Starting at $32,000*" ;
String quoted = "^" + Pattern.quote(orig) + "$" ;
System.out.println(orig + " became " + quoted) ;
}
}




Roedy Green 05-31-2011 10:53 PM

Re: Confusion about String.matches method
 
On Tue, 31 May 2011 13:15:55 -0700 (PDT), laredotornado
<laredotornado@zipmail.com> wrote, quoted or indirectly quoted someone
who said :

>
>Is there a way I can match an arbitrary string without having to
>escape everything? I tried putting my token in quotes ...


yes. See http://mindprod.com/jgloss/regex.html#AWKWARD
for various techniques of quoting.
--
Roedy Green Canadian Mind Products
http://mindprod.com
How long did it take after the car was invented before owners understood
cars would not work unless you regularly changed the oil and the tires?
We have gone 33 years and still it is rare to uncover a user who
understands computers don't work without regular backups.


laredotornado 06-01-2011 02:10 PM

Re: Confusion about String.matches method
 
On May 31, 5:53*pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Tue, 31 May 2011 13:15:55 -0700 (PDT),laredotornado
> <laredotorn...@zipmail.com> wrote, quoted or indirectly quoted someone
> who said :
>
>
>
> >Is there a way I can match an arbitrary string without having to
> >escape everything? *I tried putting my token in quotes ...

>
> yes. Seehttp://mindprod.com/jgloss/regex.html#AWKWARD
> for various techniques of quoting.
> --
> Roedy Green Canadian Mind Productshttp://mindprod.com
> How long did it take after the car was invented before owners understood
> cars would not work unless you regularly changed the oil and the tires?
> We have gone 33 years and still it is rare to uncover a user who
> understands computers don't work without regular backups.


K, thought I had this all rigured out thanks to everyone's
suggestions, but I still have this one RE that's failing and I can't
figure out why. I have

"G37 Convertible\n$45,750*".matches("^.*\\Q$45,750\\E.* $")

which returns false. If I remove the new line ("\n"), it matches, but
I can't guarantee my input won't contain new lines. How can I modify
my regular expression to match? Thanks, - Dave

Jim Janney 06-01-2011 02:41 PM

Re: Confusion about String.matches method
 
laredotornado <laredotornado@zipmail.com> writes:

> On May 31, 12:28*pm, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
> wrote:
>> laredotornado <laredotorn...@zipmail.com> wrote:
>> > Hi,
>> > I'm using Java 1.6. *How would I modify my regular expression below
>> > ^Starting at \$32,000*$

>>
>> ^Starting at \$32,000\*$
>>
>> > so that it will match the string, "Starting at $32,000*". *In other
>> > words, I have
>> > String regEx = "^Starting at \\$32,000*$";

>>
>> String regEx = "^Starting at \\$32,000\\*$";
>>
>> > String text = "Starting at $32,000*";
>> > if (!text.matches(regEx)) {
>> > * * * throw new RuntimeException("does not match");
>> > }
>> > but the exception is always thrown. *Please let me know how can I
>> > modify my reg ex to match. *Thanks, - Dave

>>
>>

>
> Is there a way I can match an arbitrary string without having to
> escape everything? I tried putting my token in quotes ...
>
> String regEx = "^Starting at (\"$32,000*\")$";
>
> but that failed to match. - Dave


String regEx= "^Starting at " + Pattern.quote("$32,000*") + "$";

--
Jim Janney

Nigel Wade 06-01-2011 03:02 PM

Re: Confusion about String.matches method
 
On 01/06/11 15:10, laredotornado wrote:

>
> K, thought I had this all rigured out thanks to everyone's
> suggestions, but I still have this one RE that's failing and I can't
> figure out why. I have
>
> "G37 Convertible\n$45,750*".matches("^.*\\Q$45,750\\E.* $")
>
> which returns false. If I remove the new line ("\n"), it matches, but
> I can't guarantee my input won't contain new lines. How can I modify
> my regular expression to match? Thanks, - Dave


Welcome to the wonderful world of RE, and strings.

To match '\n' inside an RE you need to escape the '\', because it's a
special RE character. To escape it you precede it with '\', the RE
escape character. So what you actually need in the RE is '\\n'. But '\'
is also a special character in a string, so you need to escape each '\'
in the string - with a '\'. So, to get your '\\n' in the RE you need to
have '\\\\n' in the string.

Simple?

[Anyone who claims they understand RE is just someone who hasn't yet
realized they don't].

--
Nigel Wade


All times are GMT. The time now is 07:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.