Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with a regular expresion

 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following String, String example = "context Account::deposit(am:double):void pre checkDeposit: am>999.999999 && am<1000000.000000", this is an example the string will vary, only the context word and the ( ) : :: characters are required, so first i need to extract everyting from context to ), in this example I need this: context Account::deposit(am:double), once i get this output i will continue "chopping" the expression.

by the way i'm using jakarta oro 2.0.8

[ April 11, 2008: Message edited by: Jorge Bend ]
[ April 11, 2008: Message edited by: Jorge Bend ]
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jorge, Welcome to Javaranch,
Will you please tell what have you done till now and where are you stuck
because without knowing your exact problem, a solution cannot be suggested
and yes, DoYourOwnHomework
we are not going to do it for you.

Hope this helps
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, the thing is the ) character is kind of problematic, i have somethig like this:

String someText="context Cuenta: epositar(monto ouble):void pre checkDepositar: monto>999.999999";

String regexp = "^context \\)$"; //looking for the context and ending with )

PatternCompiler compiler=new Perl5Compiler();

try {
Pattern pattern=compiler.compile(regexp);

PatternMatcher matcher = new Perl5Matcher();

if(matcher.contains(texto, pattern)){
MatchResult result = matcher.getMatch();
System.out.println(result);


}
[ April 11, 2008: Message edited by: Jorge Bend ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have some extra spaces in the expression - I assume those came from our software converting things like : D to , and now you've selected the "disable smilies" option, good job. You could edit your post again to remove the extra spaces that were inserted. But be careful to select "disable smilies" each time.

If you want everything between "context" and the ), you need to put in something to indicate that there can be more stuff in between those two features. You can insert ".*" - the "." means "any character", and the "*" means "zero of more of the thing before this symbol" . So ".*" means zero or more of any character. You can further modify this - ".*" will choose the largest possible match for .*, while ".*?" will choose the smallest possible match. This difference will only matter if there's more than one ) in the expression you're looking at. Try this:
And then try it with both

and

See what difference it makes. Which makes more sense for what you're doing? Or does it even matter?
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see a bit of how it works,Now even looks more simple, great advise. Thanks Jim. P.S. a few hours ago, just saw it at glance and implemented, now i've read it more carefully and yes, i can see the difference.
[ April 11, 2008: Message edited by: Jorge Bend ]
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again, after aplying the first regexp = ^context.*?\\) to the original String, I've got this result:
context Account::deposit(am:double)
Now I'm only trying to extract the Account part for this example, so the pattern will be something like this: [A-Z]{1}.*?\:
but the results are not quite like i've expected.
Match 1: Account:
if i don't put the : in the expression, the expression will not work, but i need to get only Account without the final : in the result.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic