Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Confusion about String.matches method

Reply
Thread Tools

Confusion about String.matches method

 
 
laredotornado
Guest
Posts: n/a
 
      05-31-2011
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
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      05-31-2011
laredotornado <> 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

 
Reply With Quote
 
 
 
 
laredotornado
Guest
Posts: n/a
 
      05-31-2011
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
 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      05-31-2011
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.
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      05-31-2011
laredotornado <> 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.

 
Reply With Quote
 
Ian Shef
Guest
Posts: n/a
 
      05-31-2011
laredotornado <> wrote in news:50016a00-9422-46d4-
8278-:

> 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) ;
}
}



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-31-2011
On Tue, 31 May 2011 13:15:55 -0700 (PDT), laredotornado
<> 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.

 
Reply With Quote
 
laredotornado
Guest
Posts: n/a
 
      06-01-2011
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
 
Reply With Quote
 
Jim Janney
Guest
Posts: n/a
 
      06-01-2011
laredotornado <> 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
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      06-01-2011
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
 
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
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Confusion over Static class and Static method! Anup Daware ASP .Net 2 02-02-2007 12:12 PM
RE: Method binding confusion Robert Brewer Python 14 05-26-2004 03:31 AM
Method binding confusion A B Carter Python 6 05-03-2004 11:06 AM
help needed with class and method confusion Cndistin Python 3 01-06-2004 09:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57