Results 1 to 10 of 10

Thread: Split a string using Regex.split

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    5

    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.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    5

    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.

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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?

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    5

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    5

    Re: Split a string using Regex.split

    Anyone who have any idea abou this?

  6. #6
    Junior Member
    Join Date
    Mar 2006
    Posts
    16

    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

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. Dim MyString As String = "error=""This is an error."" Name='My name is test' active='true' banned=false vip=""yes"""
    2.         Dim Matches As System.Text.RegularExpressions.MatchCollection = _
    3.              System.Text.RegularExpressions.Regex.Matches(MyString, "\s*\w*\s*=\s*(""[^""]*?""|'[^']*?'|[^'""]*?\s)")
    4.         For Each match As System.Text.RegularExpressions.Match In Matches
    5.             MessageBox.Show(match.Value)
    6.         Next
    Last edited by gigemboy; Jun 23rd, 2006 at 03:54 PM.

  8. #8
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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:
    1. Dim st As String = "error=""This is an error."" Name='My name is test' active='true' banned=false vip=""yes"""
    2.         Dim arr As String() = Regex.Split(st, "(\w+)\=")
    3.         Dim nv As New Specialized.NameValueCollection
    4.         For i As Int32 = 1 To arr.Length - 3 Step 2
    5.             nv.Add(arr(i), arr(i + 1).TrimEnd)
    6.         Next

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    5

    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:
    1. Dim MyString As String = "error=""This is an error."" Name='My name is test' active='true' banned=false vip=""yes"""
    2.         Dim Matches As System.Text.RegularExpressions.MatchCollection = _
    3.              System.Text.RegularExpressions.Regex.Matches(MyString, "\s*\w*\s*=\s*(""[^""]*?""|'[^']*?'|[^'""]*?\s)")
    4.         For Each match As System.Text.RegularExpressions.Match In Matches
    5.             MessageBox.Show(match.Value)
    6.         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.

  10. #10
    Junior Member
    Join Date
    Mar 2006
    Posts
    16

    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
  •  



Click Here to Expand Forum to Full Width