Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex to search a string

 
Josh Theisen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic