File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Need help with a regular expresion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need help with a regular expresion" Watch "Need help with a regular expresion" New topic

Need help with a regular expresion

Jorge Bendahan
Ranch Hand

Joined: Apr 11, 2008
Posts: 82
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

Joined: Jun 06, 2007
Posts: 2851

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

|Asking Good Questions|
Jorge Bendahan
Ranch Hand

Joined: Apr 11, 2008
Posts: 82
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();

[ April 11, 2008: Message edited by: Jorge Bend ]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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


See what difference it makes. Which makes more sense for what you're doing? Or does it even matter?

"I'm not back." - Bill Harding, Twister
Jorge Bendahan
Ranch Hand

Joined: Apr 11, 2008
Posts: 82
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

Joined: Apr 11, 2008
Posts: 82
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.
I agree. Here's the link:
subject: Need help with a regular expresion
It's not a secret anymore!