File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression

 
keshav reddy
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Martin Sergeant
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Martin.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic