wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regular expression" Watch "Regular expression" New topic
Author

Regular expression

keshav reddy
Greenhorn

Joined: May 11, 2007
Posts: 13
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
keshav reddy
Greenhorn

Joined: May 11, 2007
Posts: 13
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Martin Sergeant
Greenhorn

Joined: May 20, 2003
Posts: 24
(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

Joined: May 11, 2007
Posts: 13
thanks Martin.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular expression