Page 1 of 1

Need a Regular Expression to Seperate a String

Posted: Wed Mar 11, 2009 10:31 pm
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 ?

Re: Need a Regular Expression to Seperate a String

Posted: Thu Mar 12, 2009 1:54 am
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.

Re: Need a Regular Expression to Seperate a String

Posted: Thu Mar 12, 2009 3:09 am
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")"

Re: Need a Regular Expression to Seperate a String

Posted: Thu Mar 12, 2009 3:15 am
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);

Re: Need a Regular Expression to Seperate a String

Posted: Thu Mar 12, 2009 3:36 am
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.

Re: Need a Regular Expression to Seperate a String

Posted: Thu Mar 12, 2009 4:58 am
by a.deivanai
This Regular Expression is working fine for any combination of strings
","(?!([^()]*[()][^()]*[()])*$)

Re: Need a Regular Expression to Seperate a String

Posted: Thu Mar 12, 2009 5:46 am
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!
; )