• 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

Regular expression

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Can you please help me in writting the regular expressions for the following requirements:

1. Input string : "1a23MyFunc(ab)0123MyFunc(1234)000"

Need to split the string with: MyFunc(anyvalue)

Required output is (string array): 1a23,0123,000


2. InputString : "abcMyFunc(ab)xyzMyFunc(1234)000"

Required output is (value passed to MyFunc method) : ab, 1234

Thanks,
Keshav
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried so far? Can't give you a hint in the right direction, if we don't know where are you stuck.

Henry
 
keshav reddy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for the reply.
For the first requirement, I tried to split by using split method as below, but not giving exact result:

StaticString string : "1a23MyFunc(ab)0123MyFunc(1234)000"
String[]Key = StaticString.split("MyFunc*");

output is : abc , (a)xyz, (1234)000
But I need: abc, xyz, 000

I tried to split with MyFunc(*), but it giving runtime error: Dangling meta character '*'

Can you please give me some assistance in solving this.

Thanks,
Keshav
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

keshav reddy wrote:
For the first requirement, I tried to split by using split method as below, but not giving exact result:

StaticString string : "1a23MyFunc(ab)0123MyFunc(1234)000"
String[]Key = StaticString.split("MyFunc*");

output is : abc , (a)xyz, (1234)000
But I need: abc, xyz, 000



I think it may be best to start from scratch, with a tutorial about regular expressions. You have some concepts about regex that are clearly wrong.

"MyFunc*" means to match "MyFun" followed by zero or more "c"s. This is clearly not what you want. Take a look at a regex tutorial, and specifically, how the "*" modifier works.


Henry
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1) The first pattern you require would be "MyFunc\\(.+?\\)". If this is used in String.split() it should
give you the desired result. The brackets () have to be preceded by \\ in order to differentiate them
the grouping operators which are also brackets. The .+? means one or more of any character can
occur in the brackets. The reluctant quantifier +? is used instead of the greedy quantifier +, else
MyFunc(ab)0123MyFunc(1234) would be matched instead of each individual instance of MyFunc(x)

(2) For the second part of your question I think something like the following will work:


In this case anything between the brackets of myFunc is grouped and can be retrieved by m.group(1)
(m.group() or m.group(0) will retrieve the full match)

However, having said all this, reluctant quantifiers and using grouping to capture contents is not on the exam
(Objective 3.5), so this is probably more information than you need to know

Martin
 
keshav reddy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Martin.
 
All of the world's problems can be solved in a garden - Geoff Lawton. 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