|
-
Jun 22nd, 2006, 12:27 AM
#1
Thread Starter
New Member
Split a string using Regex.split
I have this string:
error="This is an error." Name='My name is test' active='true' banned=false vip="yes"
what regex expression would make it split like this(using Regex.Split):
error=”This is an error.”
Name='My name is test'
active='true'
banned=false
vip="yes"
I'm using 1.x .Net version.
Thanks.
Last edited by page5; Jun 22nd, 2006 at 01:23 AM.
-
Jun 22nd, 2006, 02:15 AM
#2
Thread Starter
New Member
Re: Split a string using Regex.split
Adding [ and ] and every end, so it becomes "[error=”This is an error.”] [Name='My name is test'] [active='true'] [banned=false] [vip="yes"]" i can split it using \s*(?<Value1>[^]]+)\s*\=\s*(?<Value>[^]]+)\s*\]
please help me modify that expression so it could split error="This is an error." Name='My name is test' active='true' banned=false vip="yes" into
error=”This is an error.”
Name='My name is test'
active='true'
banned=false
vip="yes"
anyone well-versed with regex can help me?
THanks.
Last edited by page5; Jun 22nd, 2006 at 03:29 AM.
-
Jun 22nd, 2006, 12:40 PM
#3
Re: Split a string using Regex.split
Well one of the problems are the mixes of using double quotes, single quotes, and then no quotes on the parts... is there any way it can be changed to be more consistent?
-
Jun 22nd, 2006, 09:01 PM
#4
Thread Starter
New Member
Re: Split a string using Regex.split
Unfortunately no. It can be a mix of double, single or no quotes at all.
Last edited by page5; Jun 22nd, 2006 at 11:17 PM.
-
Jun 23rd, 2006, 02:22 AM
#5
Thread Starter
New Member
Re: Split a string using Regex.split
Anyone who have any idea abou this?
-
Jun 23rd, 2006, 05:20 AM
#6
Junior Member
Re: Split a string using Regex.split
I have work on this since I have the same problem. Using this \s*([^( )]+)\s*\=.*?([^'( )]+)\s*? I was able to split it into
error=”This
Name='My
active='true'
banned=false
vip="yes"
Without the spaces in error=”This is an error.”(error=”Thisisanerror.”)
Name='Mynameistest' (Name='Mynameistest') it splits it into
error=”Thisisanerror.”
Name='Mynameistest'
active='true'
banned=false
vip="yes"
I know I'm almost there. You can, or someonce can, modify this to fit your needs. Let me know if you figure it out.
Using VB.Net 2003
.Net version: 1.x
-
Jun 23rd, 2006, 03:47 PM
#7
Re: Split a string using Regex.split
Alright... I played with it a little while since I lubb regex and string manipulation, and I got it working. The regex expression can look a little cryptic, and I don't like explaining all the little details because it can take a while (that and the fact that sometimes I just get lucky ), but the below should display each section in a messagebox..
VB Code:
Dim MyString As String = "error=""This is an error."" Name='My name is test' active='true' banned=false vip=""yes"""
Dim Matches As System.Text.RegularExpressions.MatchCollection = _
System.Text.RegularExpressions.Regex.Matches(MyString, "\s*\w*\s*=\s*(""[^""]*?""|'[^']*?'|[^'""]*?\s)")
For Each match As System.Text.RegularExpressions.Match In Matches
MessageBox.Show(match.Value)
Next
Last edited by gigemboy; Jun 23rd, 2006 at 03:54 PM.
-
Jun 23rd, 2006, 04:26 PM
#8
Re: Split a string using Regex.split
It might be easier for you to parse your data into a dictionary so you can easily lookup values later in your program
VB Code:
Dim st As String = "error=""This is an error."" Name='My name is test' active='true' banned=false vip=""yes"""
Dim arr As String() = Regex.Split(st, "(\w+)\=")
Dim nv As New Specialized.NameValueCollection
For i As Int32 = 1 To arr.Length - 3 Step 2
nv.Add(arr(i), arr(i + 1).TrimEnd)
Next
-
Jun 25th, 2006, 09:24 PM
#9
Thread Starter
New Member
Re: Split a string using Regex.split
 Originally Posted by gigemboy
Alright... I played with it a little while since I lubb regex and string manipulation, and I got it working. The regex expression can look a little cryptic, and I don't like explaining all the little details because it can take a while (that and the fact that sometimes I just get lucky  ), but the below should display each section in a messagebox..
VB Code:
Dim MyString As String = "error=""This is an error."" Name='My name is test' active='true' banned=false vip=""yes"""
Dim Matches As System.Text.RegularExpressions.MatchCollection = _
System.Text.RegularExpressions.Regex.Matches(MyString, "\s*\w*\s*=\s*(""[^""]*?""|'[^']*?'|[^'""]*?\s)")
For Each match As System.Text.RegularExpressions.Match In Matches
MessageBox.Show(match.Value)
Next
Thanks it works.
One problem though. When you have this error="This is an error." Name='My name is test' active='true' banned=false vip=yes or error="This is an error." Name='My name is test' active=true banned=false vip=yes. It won't be able to match vip=yes. Is there anyway also it will be able to spli correctly if for example we have error="This is an error." Name='My name is test' active=true' banned=false vip=yes
THanks for the help.
Last edited by page5; Jun 26th, 2006 at 05:49 AM.
-
Jun 27th, 2006, 05:09 AM
#10
Junior Member
Re: Split a string using Regex.split
[edit]
Sorry, wrong posts.
[/edit]
Last edited by h1dden; Jun 27th, 2006 at 05:14 AM.
Using VB.Net 2003
.Net version: 1.x
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|