?
Solved

regular expressions in Java

Posted on 2003-03-05
24
Medium Priority
?
268 Views
Last Modified: 2010-03-31
ok, i dont think that anyone will be able to help with this (without seeing entire code), but I have tried everything and cant explain it so i thought i would give this a try.... it may be difficult to explain but i will try -

i am trying to do a pattern test for a regular expression against a string that would have something like the following format : "x+10"

the code i have used is below and works for "x+9" (ie any single digit int).  however within my code it will not work for anything greater than 9.  
The confusing part is when i take this section of code out and place it a new class of its own for testing purposes, it works perfectly, and matches for "x+100" etc.

I cant find no reason why there should be a difference - the code is identical as it was copied & pasted.?????

here is the code:


Pattern p2 = Pattern.compile("[a-zA-Z]{1,}[-][a-zA-Z]{1,}");
Matcher m2 = p2.matcher(test);


if(m2.find())
{
//debug System.out.println("-ve Match6 for " +   existingValue + " against pattern [0-9]{1,}");
                   
int old = Integer.parseInt(existingValue);
int current = isolateValue(test);
int newValue = old - current;

String newVarValue = Integer.toString(newValue);
setVarValue(newVarValue);
}
0
Comment
Question by:need_help_pls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 11
24 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8075332
I might be missing something, but i can see no relation between that compiled pattern and what you're trying to match whatsoever.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8075871
What would you *like* to match?
0
 

Author Comment

by:need_help_pls
ID: 8079311
Sorry!!!!  I pasted the wrong code this should be right:


if(addM.find())
{
// debug
System.out.println("Match2 for " + test + " against pattern [ a-zA-Z ]{1,}[+][0-9]{1,}");
               
...
}

where
Pattern addP = Pattern.compile("[a-zA-Z ]{1,}[+][0-9]{1,}");          
Matcher addM = addP.matcher(test);


and test would take form of something along the lines of "x+9", which works... or "x+100", which does not work in my main program, but does when places in a separate class on its own??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8079350
>>or "x+100", which does not work in my main program

Can you post the code that proves this?
0
 

Author Comment

by:need_help_pls
ID: 8079356
>>>also, have the same type of strange prob with this code:

//String patternStr = "[ ][+|-|*|/][ ]";
String patternStr = "[+]";

String[] fields = varValue.split(patternStr);
                           
tempVar = (fields[0]);
tempVal = (fields[1]);


>>>the above will only work for "+"...  so the String "x+1" splits into "x" and "1"... however it will not work for "-", "*" or "/" , it doesn't match at all???
Should it not match + or - or * or / whatever it finds first???  therefore i cannot process strings like "x-9", "x*9" or "x/9"???
0
 

Author Comment

by:need_help_pls
ID: 8079375
>>or "x+100", which does not work in my main program

Can you post the code that proves this?


>>> the code is above.... what i mean is that I know it doesnt work because when i input "x+9" the if block executes as desired... but when i enter "x+10", it does not match and just skips on to the next section of the code?
0
 

Author Comment

by:need_help_pls
ID: 8079411
import java.util.regex.*;


public class TestRegExpr
{
  public static void main(String[] args)throws Exception
  {

    String input = "x+1000";

    Pattern p = Pattern.compile("[ a-zA-Z ]{1,}[+][0-9{1,}");
    Matcher m = p.matcher(input);

    if (m.find())
    {
      System.out.println("matched");
    }
    else
    {
      System.out.println("not matched for " + input);
    }
  }
}

>>> the above works ie matches. but when i use this same code in my proper class as part of another method, it does not match??????
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8079485
Let's take one thing at a time.

The last example is missing its closing square bracket in the character class. Please confirm it is working after correcting this.
0
 

Author Comment

by:need_help_pls
ID: 8079520
ok, thats strange, must have done something odd when pasting it because the code itself is fine -
(Pattern p = Pattern.compile("[ a-zA-Z ]{1,}[+][0-9]{1,}");)
that code, TestregExpr, is the class that works perfectly - just that this code does not work when it my original class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8079540
Well it's no good posting code that *is* working! Please show code that is *not* working - along with the String you're trying to match.
0
 

Author Comment

by:need_help_pls
ID: 8079573
what i am trying to explain is that it IS the same code... if you look at what I posted as the incorrect code and the working code you will see they are the same - the problem is that when I have the code in a class of its own it works - and when i have it as part of my original class it doen not...  

here is the code from my original class again (as posted above)

if(addM.find())
{
// debug
System.out.println("Match2 for " + test + " against pattern [ a-zA-Z ]{1,}[+][0-9]{1,}");            
...
}

where
Pattern addP = Pattern.compile("[a-zA-Z ]{1,}[+][0-9]{1,}");          
Matcher addM = addP.matcher(test);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8079714
I realise that, but effectively, if it's in a different context, it's different code. So you were right originally, we need to see it in context, otherwise it's not possible to comment.
0
 

Author Comment

by:need_help_pls
ID: 8089831
teh second if statement is the relevant one .. i added the others just to put it in context, however more statements of the same type follow the one in question which is the text of addM



Pattern p = Pattern.compile("[0-9]{1,}[^.]");
Matcher m = p.matcher(test);
         
Pattern addP = Pattern.compile("[a-zA-Z ]{1,}[+][ 0-9 ]{1,}");          
Matcher addM = addP.matcher(test);          

if(m.find())
{
//debug System.out.println("Match for " + test + " against pattern \\d");
setVarValue(processor.getVarValue());
}
else if(addM.find())
{
// debug
System.out.println("Match2 for " + test + " against pattern [ a-zA-Z ]{1,}[+][0-9]{1,}");
               
String existingValue = returnMemoryValue(varName);
Pattern existP1 = Pattern.compile("[0-9]{1,}");
Matcher existM1 = existP1.matcher(existingValue);
               
//debug
System.out.println("existingValue" + existingValue);
               
if(existM1.find())
{
//debug
System.out.println("Match3 for " + existingValue + " against pattern [0-9]{1,}");
                   
int old = Integer.parseInt(existingValue);
int current = isolateValue(test);
int newValue = old + current;
System.out.println("new " + newValue + " = old " + old + " + current "+current);
String newVarValue = Integer.toString(newValue);
setVarValue(newVarValue);
}
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8089928
Well, of course your second one being tested at all depends on the first having failed. Can you tell me what the input is that you are expecting the first not to match and the second to match?
0
 

Author Comment

by:need_help_pls
ID: 8090222
should give you the background for that.  the programme is designed to be a learning tool for novice programmers (irony or wot!!) and acts almost as a compiler. the user can input code line and by line and have it processed with various procedures running to explain the code they have entered.  what i am attempting at this stage is assignment statements and subsequently each line inputted by the user must be tested for the following types:
int x = 4;
x = x + 1;
x += 1;
int y = x + 1; etc....
whatever the input once this method that the above code runs in is called the inputted text at this point will be of the form "varName =  something" where something may be either "int" eg "6"; "string" - "x + 6", or "string" "x + y" where x and y would be previously initialized vars that their values can be obtained by calling returnMemoryValue()

therefore at this point the code above is a long list of it statements checking the "test" section of the input (ie the right-hand side of the statement).  I have added a println to each if block to identify what statements are matched....  

Pattern addM, will correctly match "x + 9" as required, however, will not match anything that has a 2char digit eg "x + 10"?!?!?!?!?  no idea why (as explained same code works else where)

as for the first pattern test that merely checks that the right-hand side of the statement is a straight-forward assignment to an int value, and this works correctly for digit of all lengths....????
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8090593
Can't test now to 'prove' but:

'x + 9'  m does not match, so addM is tried
'x + 10' m does match so addM is not tried
0
 

Author Comment

by:need_help_pls
ID: 8090916
so... your suggestion is to let addM check for both then I guess.... will try that - thank you!!

sorry to drag it out but any ideas on the operator issue as aforementioned?!?!

Final year project deadline only a forthnight away so any idea that would help solve the mystery would be great - i think that i have just been looking at this code too long, im missing all these obvious mistakes, but none of my class mates seem to spot them either so I dont feel too bad!!!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8091004
>>the mystery

What mystery?
0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8092118
Apache's ORO project is probably more flexable than java.util.regexp

http://jakarta.apache.org/oro/index.html
0
 

Author Comment

by:need_help_pls
ID: 8093446
this mystery:  why the commented statement below doesnt work????
but that the single operator match of "+2 does, in fact if i replace it with an "-", the split does not work!?!?!


//String patternStr = "[ ][+|-|*|/][ ]";
String patternStr = "[+]";

String[] fields = varValue.split(patternStr);
                         
tempVar = (fields[0]);
tempVal = (fields[1]);


>>>the above will only work for "+"...  so the String "x+1" splits into "x" and "1"... however it will not work for "-", "*" or "/" , it doesn't match at all???
Should it not match + or - or * or / whatever it finds first???  therefore i cannot process strings like "x-9", "x*9" or "x/9"???
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8093617
Don't forget that minus, in a character class, is a reserved character, representing a range.

Again, can't test this, but try:

String patternStr = "\s*?+|-|*|/\s*?";
0
 

Author Comment

by:need_help_pls
ID: 8102032
String patternStr = "\s*?+|-|*|/\s*?";  - this returned some meta char errors...
but i did sort out the problem just by creating some messy code.... but it works - thank you sooooo much for all your help!!!!!
0
 

Author Comment

by:need_help_pls
ID: 8102036
thank you!!!!!!!!!!!!!!!!!!!!!!!!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8102064
>>this returned some meta char errors...

Yes, i was afraid of that. Try escaping some, starting with '*'. If that doesn't compile, try escaping '+' as well.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question