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.
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.
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?
Re: Split a string using Regex.split
Unfortunately no. It can be a mix of double, single or no quotes at all.
Re: Split a string using Regex.split
Anyone who have any idea abou this?
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.
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
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
Re: Split a string using Regex.split
Quote:
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.
Re: Split a string using Regex.split
[edit]
Sorry, wrong posts.
[/edit]