wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Regex to search a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regex to search a string" Watch "Regex to search a string" New topic
Author

Regex to search a string

Josh Theisen
Ranch Hand

Joined: Sep 23, 2011
Posts: 31
Hey everyone. I've pasted my little project I"m working on so far... I have a text file of some code and I need to go through it line by line and
find all of the for loops in it. I've got it so that I can go through all of the lines, but I'm stuck on the regex part. Basically, if the line has "for" followed by a left parenthesis, it should qualify as a for loop. If it qualifies, it should print out that line.
I tried using but that obviously doesn't work in all cases since the word for can be used in comments, or other strings.

I've tried looking up what would be the correct regex expression but I'm not coming up with much.

Thanks.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

1) Comments would either start with // or /* or /**. Read the line, trim it and then check if it starts with these (1st two actually, 3 will be covered by two)
2) for can be followed by { or for can be followed by a white space and then a {
You can run these two checks without regex.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If you're just doing this for educational purposes to fiddle with regex, that's fine, but just be aware that regex is not a suitable tool for this kind of thing, and in fact I think it's impossible for regex to do it.

And even for the simple case, you have to decide whether you're interested in unusual but syntactically legal examples.



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Maneesh Godbole wrote:1) Comments would either start with // or /* or /**. Read the line, trim it and then check if it starts with these


Not just start of line.


2) for can be followed by { or for can be followed by a white space and then a {
You can run these two checks without regex.


The for keyword must be followed by ( (with possible intervening whitespace and/or comments)

Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

Maneesh Godbole wrote:1) Comments would either start with // or /* or /**. Read the line, trim it and then check if it starts with these (1st two actually, 3 will be covered by two)
2) for can be followed by { or for can be followed by a white space and then a {
You can run these two checks without regex.


This does not cope with a comment that is tacked on the end of a statement. e.g.


As specified I don't think this problem is suitable for a regex approach and unless this is a homework exercise one would do better to formally define the language and write a parser. 'javacc' springs to mind.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

@OP: Even if you are just doing this for an educational exercise, your problem at this point doesn't really have anything to do with regex. Rather, your problem is that you haven't spelled out the rules clearly, precisely, and completely.
Josh Theisen
Ranch Hand

Joined: Sep 23, 2011
Posts: 31
Thanks for the replies, everyone.
As some of you may have guessed, this is for educational purposes, but we are not limited to using only regex. I had thought that regex would have been the easiest way to
check for predicates (for, if, else, else if, etc). From what I've gathered, it would be best to write a separate parser for this type of problem.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex to search a string
 
Similar Threads
Regarding reading a file in a jsp
Reading one line from a text
how to go to last line of a file and skipping the rest?
to read lines inside brackets
Why do i get NoClassDefFound error??