Need a Regular Expression to Seperate a String

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ash1983
Forum Newbie
Posts: 1
Joined: Wed Mar 11, 2009 10:17 pm

Need a Regular Expression to Seperate a String

Post by ash1983 »

Hi,
I have a String Like this
str = "450::Must be 9 numbers::("VILLEPA,16635")","450::Must be 9 numbers::(""VILLEPARLE)","276::Must not be blank::()","450::Must be 9 numbers::(88,8::8,88,88)","1187::Not found in eligibility table::(8888:::88,88"8")"

I want the Output like the below,
str1 = 450::Must be 9 numbers::("VILLEPA,16635")
str2 = 450::Must be 9 numbers::(""VILLEPARLE)
str3 = 276::Must not be blank::()
str4 = 450::Must be 9 numbers::(88,8::8,88,88)
str5 = 1187::Not found in eligibility table::(8888:::88,88"8")

I need a Regex for achiving the above output.
Basically I need a Regex which should seperate the string by using the comma(,) [which is not present inside the bracket() ] as delimeter.

Rule 1: There is no limitations for combinations of strings.In the above example i have combination of 5 strings.Sometimes it may contain only one string and sometimes it may have more than 10 strings.
Rule 2: Inside the bracket() ,it may have all kind of characters(alphanumeric and all the special chars)
(E.g) 1187::Not found in eligibility table::("" $ test %&^ @# '8888' ("testing")::('fact'):88,88"8")

Can anyone please help me on this Regex ?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Need a Regular Expression to Seperate a String

Post by prometheuzz »

Are you the same person as in this tread: viewtopic.php?f=38&t=96647?
If so, why did you create a new account?

Anyway, you can split on the the following substring:

Code: Select all

","
(quote comma quote)
and you have your string as you wanted.
a.deivanai
Forum Newbie
Posts: 8
Joined: Wed Mar 11, 2009 5:46 am

Re: Need a Regular Expression to Seperate a String

Post by a.deivanai »

Regex reg = ","(?!([^()]*[()][^()]*[()])*[^()]*$)

string str = "450::Must be 9 numbers::("VILLEPA,16635")","450::Must be 9 numbers::(""VILLEPARLE)","276::Must not be blank::()","450::Must be 9 numbers::(88,8::8,88,88)","1187::Not found in eligibility table::(8888:::88,88"8")"

reg.Split(str);

It is just displaying the input string as output.It is not splitting the strings.

"450::Must be 9 numbers::("VILLEPA,16635")","450::Must be 9 numbers::(""VILLEPARLE)","276::Must not be blank::()","450::Must be 9 numbers::(88,8::8,88,88)","1187::Not found in eligibility table::(8888:::88,88"8")"
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Need a Regular Expression to Seperate a String

Post by prometheuzz »

a.deivanai wrote:...
reg.Split(str);

It is just displaying the input string as output.It is not splitting the strings.
...
That's because you're not using it correctly.
Have a look at the help pages for the split method:

http://www.php.net/preg_split

Edit: wait, it's you again...
In C#, you should do it like this:

Code: Select all

string[] substrings = Regex.Split(input, pattern);
a.deivanai
Forum Newbie
Posts: 8
Joined: Wed Mar 11, 2009 5:46 am

Re: Need a Regular Expression to Seperate a String

Post by a.deivanai »

Yeah,the syntax is Correct in C#.Net
The Regular Expression which you gave is just finding the comma(,) and splitting the strings
But I need a Regular Expression Which should find the comma(,) which is not present in the brackets() and then it should split the strings.

(E.g:1) abcd::("acd,gf(sdsd)")","desa::("asd,))
It is splitting correctly here as
abcd::("acd,gf(sdsd)")"
"desa::("asd,))

(E.g:2) "450::Must be 9 numbers::("VILLEPA,16635")","450::Must be 9 numbers::(""VILLEPARLE)","276::Must not be blank::()","450::Must be 9 numbers::(88,8::8,88,88)","1187::Not found in eligibility table::(8888:::88,88"8")"

Here it is not splitting the strings correctly.It is just giving the input string as output.

Can you just check this input string with the Regular Expression ","(?!([^()]*[()][^()]*[()])*[^()]*$) in PHP code and Please let me know.
a.deivanai
Forum Newbie
Posts: 8
Joined: Wed Mar 11, 2009 5:46 am

Re: Need a Regular Expression to Seperate a String

Post by a.deivanai »

This Regular Expression is working fine for any combination of strings
","(?!([^()]*[()][^()]*[()])*$)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Need a Regular Expression to Seperate a String

Post by prometheuzz »

a.deivanai wrote:This Regular Expression is working fine for any combination of strings
","(?!([^()]*[()][^()]*[()])*$)
I highly doubt that, but if you're content with it, you go ahead an use it!
; )
Post Reply