File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes regular expression problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regular expression problems" Watch "regular expression problems" New topic
Author

regular expression problems

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
having trouble starting a regular expression or telling the JVM to recognize the code as a regular expression and not a variable.

here are the errors I am getting:



C:\Documents and Settings\ben\Desktop\park>javac BenHultinDateDriver.java
.\Date.java:54: illegal start of expression
if (monthStr == [a-zA-Z]) {
^
.\Date.java:54: not a statement
if (monthStr == [a-zA-Z]) {
^
.\Date.java:54: ';' expected
if (monthStr == [a-zA-Z]) {
^
.\Date.java:73: illegal start of expression
if (dayStr == [a-zA-Z]) {
^
.\Date.java:73: not a statement
if (dayStr == [a-zA-Z]) {
^
.\Date.java:73: ';' expected
if (dayStr == [a-zA-Z]) {
^
.\Date.java:54: cannot find symbol
symbol : variable a
location: class Date
if (monthStr == [a-zA-Z]) {
^
.\Date.java:54: cannot find symbol
symbol : variable zA
location: class Date
if (monthStr == [a-zA-Z]) {
^
.\Date.java:54: cannot find symbol
symbol : variable Z
location: class Date
if (monthStr == [a-zA-Z]) {
^
.\Date.java:73: cannot find symbol
symbol : variable a
location: class Date
if (dayStr == [a-zA-Z]) {
^
.\Date.java:73: cannot find symbol
symbol : variable zA
location: class Date
if (dayStr == [a-zA-Z]) {
^
.\Date.java:73: cannot find symbol
symbol : variable Z
location: class Date
if (dayStr == [a-zA-Z]) {
^
12 errors

C:\Documents and Settings\ben\Desktop\park>



Here is my code:



There is more but I felt this is mainly the issue.

Thanks a lot for your help
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

The culprit lies in this line:

String should be compared with the string.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 459
    
    1
Read this for regular expression in string.


Vinod Tiwari | Twitter
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You also seem to believe that regular expressions get special syntax in Java (like in Ruby etc.), but you are incorrect; they do not.

Any search for java +regular expressions would have answered this basic syntactic question in about 30 seconds, no?
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks for the help...

"String should be compared with the string."

What do you mean by this?

saying I should compare monthStr with monthStr?

or

saying I should compare monthStr with the value being held by monthStr?

first one, not checking for a match checking if any letters exist in the string variable

second one, I dont know the value of monthStr its a user input.


I have been doing internet searches before and after the post as was mentioned to solve the problem. My problem is I am not sure which package to import for my specific problem and I am not totally sure about how matcher and matches works. My best idea of what I read is:



Mind filling me in on what I am missing?

Thanks a lot
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

A typical usage:

As previously noted first argument should be of type String.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks for the response,

mind explaining how that could help me in checking monthStr for any letters contained in it? I just dont understand what its doing. I have read the articles online as well and I just need some clarification on the topic so I can get it better.

Thanks a lot for the help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18


Just change that one line into the method you and Vijitha mentioned:
However, that check isn't what you want. You don't want to check if monthStr contains a letter; if it doesn't it can still contain whitespace, punctuation marks, basically anything but letters. What you want is to really check on not being digits:
This will execute the if-statement body if monthStr is anything but exactly one character. Check out how to change that regular expression to allow for one or more matches. Then you get what you want: if monthStr is not one or more digits (a.k.a. not a number) then execute the body.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regular expression problems