• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Using a regex to get function parameters

 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am new to regex, can someone tell me how do i write regex which will return me below 2 seperate strings from string "concat(hello,world)"

hello
world

Would appreciate if you can point out a good tutor to learn regex.

I tried below but it's returning me (hello,world) i want "hello" from the first group and "world" from the second group and so on depending on the number of parameters passed to the function.

 
Marshal
Posts: 75723
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you get that regex from? It looks as i you had been guessing. I suggest you pass the entire String to a Scanner constructor. Start off with it using \( as a delimiter then change it after the first token to ,\s*|\) or something similar, and keep going while myScanner.hasNext().
I like the Java™ Tutorials as an introduction to regexes.
 
Saloon Keeper
Posts: 7327
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like \p{L}*\((\p{L}*),(\p{L}*)\) should do the trick. It assumes that there is no white space, that there are exactly two parameters, and that the parameter names contain only letters. Remember that backslashes need to be escaped (i.e. doubled) in Java string literals. You can test regexps on sites like http://www.regexplanet.com/advanced/java/ - much simpler than writing code that tests them.
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic