Link to home
Start Free TrialLog in
Avatar of need_help_pls
need_help_pls

asked on

regular expressions in Java

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);
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

I might be missing something, but i can see no relation between that compiled pattern and what you're trying to match whatsoever.
What would you *like* to match?
Avatar of need_help_pls
need_help_pls

ASKER

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??
>>or "x+100", which does not work in my main program

Can you post the code that proves this?
>>>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"???
>>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?
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??????
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.
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.
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.
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);
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.
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);
}
}
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?
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....????
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!!!!!
>>the mystery

What mystery?
Apache's ORO project is probably more flexable than java.util.regexp

http://jakarta.apache.org/oro/index.html
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"???
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*?";
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!!!!!
thank you!!!!!!!!!!!!!!!!!!!!!!!!!!
>>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.