• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Regex - Find a word  RSS feed

 
Vincent Tomas
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have been tasked to parse out a variable from c++ source files. If you can imagine, developers have different ways of doing things. However, there is a pattern that I could work with but I can't seem to find the right regular expression.

The text that I parsed out so far looks similar to this
snippet 1


or it could be this
snippet 2


or it could be this
snippet 3


I am trying to extract keyVar, keyVariable, myKeyVariable. That particular variable could be named any number of ways. How would I extract it? If I used the ...\\w+\p{Punct}, snippet 2 would be correct but not snippet 3.

Any help is appreciated.

Thanks.
Vince
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent,
So the first step is to articulate what you are trying to match. I think it is the third method argument. Is this correct?

If so, there are many ways of doing this. Here's the first one that came to mind (that works)



I'm big on dividing my reg exps into pieces to make it more readable. I went with the approach of finding the second and third variables and then saying anything can come before that. The "?" is key to make sure we get the second and third rather than ones later on in the string.
 
Vincent Tomas
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thank you for reply to my post. I apologize for not replying quickly and yes it is the third method argument that I would like to parse out. I tried out your code and I really liked your advice in dividing your regular expression. However, maybe I wasn't clear on my explanation because your code doesn't extract the third variable, which is my goal. Unless, I am printing it out incorrectly? The only way I could think of extracting the third method argument is by doing a string split() using the comma as the delimiter after your code. Please see below:



I will keep trying trying your code. Thank you for helping out.

Vincent
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!