• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Java Regex - Find a word

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 40679
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic