problem with regex for string searching

S

strus_82

Hi,
I'm trying to parse string like the one bellow:


a = "FUNC_CALL return_value = ( ( ( b . method_call
( something ) ) ) ) ) ";

I want to find starting point for "b . method_call (" of this
substring; I've tried the following:


String regex = "FUNC_CALL.*?([a-zA-Z_$][a-zA-Z_$0-9]* (\\. [a-zA-Z_$]
[a-zA-Z_$0-9]*)? \\()";


int id = a.indexOf(regex);

but there is no match :/ Could anyone help me with this issue?

-Marcin
 
S

strus_82

Hi,
I'm trying to parse string like the one bellow:

a = "FUNC_CALL return_value = ( ( ( b . method_call
( something ) ) ) ) ) ";

I want to find starting point for "b . method_call (" of this
substring; I've tried the following:

String regex = "FUNC_CALL.*?([a-zA-Z_$][a-zA-Z_$0-9]* (\\. [a-zA-Z_$]
[a-zA-Z_$0-9]*)? \\()";

int id = a.indexOf(regex);

but there is no match :/ Could anyone help me with this issue?

-Marcin

And one more issue, this was an example - in general there can be
other situation e.g "FUNC_CALL method_call ( )";

I need to find "method_call" (and if there is some object before the
method call then also this object - "some_object . some_method_call
(" );

for sure in string there will be at least: "FUNC_CALL " +
"some_method_call" + " ("
 
T

Thomas Kellerer

strus_82 wrote on 10.01.2009 18:45:
I'm trying to parse string like the one bellow:
>
String regex = "FUNC_CALL.*?([a-zA-Z_$][a-zA-Z_$0-9]* (\\. [a-zA-Z_$]
[a-zA-Z_$0-9]*)? \\()";

int id = a.indexOf(regex);

but there is no match :/ Could anyone help me with this issue?

indexOf will *not* use a regex.

You need to use a Pattern and Matcher to use the regex:

Pattern p = Patter.compile("your regex here");
Matcher m = p.matcher(a);

Then use Matcher.find() to test if your regex is part of the input string


The regex tutorial might be a good idea to read as well:
http://java.sun.com/docs/books/tutorial/essential/regex/index.html

Thomas
 
J

John B. Matthews

strus_82 said:
And on 10 Sty, 18:45, strus_82 <[email protected]> added: [...]
I'm trying to parse string like the one bellow:

a = "FUNC_CALL return_value = ( ( ( b . method_call
( something ) ) ) ) ) ";

Is this syntactically correct for the language you're parsing?
I want to find starting point for "b . method_call (" of this
substring; I've tried the following:

String regex = "FUNC_CALL.*?([a-zA-Z_$][a-zA-Z_$0-9]* (\\. [a-zA-Z_$]
[a-zA-Z_$0-9]*)? \\()";

int id = a.indexOf(regex);

but there is no match :/ Could anyone help me with this issue?
[...]
And one more issue, this was an example - in general there can be
other situation e.g "FUNC_CALL method_call ( )";

I need to find "method_call" (and if there is some object before the
method call then also this object - "some_object . some_method_call
(" );

for sure in string there will be at least: "FUNC_CALL " +
"some_method_call" + " ("

If parentheses may be nested arbitrarily and malformed inputs must be
rejected, then a recursive descent parser may be more straightforward:

<http://en.wikipedia.org/wiki/Recursive_descent_parser>
<http://sites.google.com/site/drjohnbmatthews/enumerated-functions>
 
S

strus_82

strus_82 wrote on 10.01.2009 18:45:
I'm trying to parse string like the one bellow:
String regex = "FUNC_CALL.*?([a-zA-Z_$][a-zA-Z_$0-9]* (\\. [a-zA-Z_$]
[a-zA-Z_$0-9]*)? \\()";
int id = a.indexOf(regex);
but there is no match :/ Could anyone help me with this issue?

indexOf will *not* use a regex.

You need to use a Pattern and  Matcher to use the regex:

Pattern p = Patter.compile("your regex here");
Matcher m = p.matcher(a);

Then use Matcher.find() to test if your regex is part of the input string

The regex tutorial might be a good idea to read as well:http://java.sun.com/docs/books/tutorial/essential/regex/index.html

Thomas

of course :/
thx a lot, it's working

br,
Marci
 
R

Roedy Green

Hi,
I'm trying to parse string like the one bellow:


a = "FUNC_CALL return_value = ( ( ( b . method_call
( something ) ) ) ) ) ";

When your text has balanced nested delimiters a regex runs out of
steam. You need a parser.

See http://mindprod.com/jgloss/parser.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top