• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 79179
377
  • 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: 7585
176
  • 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.
 
This one time, at bandcamp, I had relations with a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic