Jéjé,
You could try something like:
Const pattern As String =
"^(?'key'[\w\s]+|\[[\w\s=]+\])=(?'value'[\w\s]+|\[[\w\s=]+\])$"
Static lineRegex As New Regex(pattern, RegexOptions.Compiled)
Const line1 As String = "Name1 = Value1"
Const line2 As String = "[Name2=CFODEV]=[Value2=CFO66]"
Const line3 As String = "Name1=Value1"
Const line4 As String = "[Name2=CFODEV]=[Value2=CFO66]"
Const line5 As String = "Name3=[Value3=DFO21]"
Const line6 As String = "[Name4=CFOESX]=Value4"
Const line7 As String = "[Name2 with space=CFODEV]=[Name2 with
space=CFO66]"
Const line8 As String = "this is a long text=this is a very long
text"
Dim lines() As String = {line1, line2, line3, line4, line5, line6,
line7, line8}
For Each line As String In lines
Dim match As Match = lineRegex.Match(line)
Debug.WriteLine(line, "line")
Debug.WriteLine(match.Success, "success")
If match.Success Then
Debug.Indent()
Debug.WriteLine(match.Groups("key"), "key")
Debug.WriteLine(match.Groups("value"), "value")
Debug.Unindent()
End If
Next
The appove pattern assumes that leading & trailing spaces are part of the
token, you could use String.Trim to remove them. With effort you should be
able to modify the pattern to support returning the info between the []
instead of including the []. I will leave that as an exercise for you. Again
you could use String.Trim to remove the [] also...
A tutorial & reference on using regular expressions:
http://www.regular-expressions.info/
The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/de...geElements.asp
Hope this helps
Jay
"Jéjé" <willgart_A_@hotmail_A_.com> wrote in message
news:Ol**************@TK2MSFTNGP10.phx.gbl...
what'S appends if I have spaces like this:
[Name2 with space=CFODEV]=[Name2 with space=CFO66]
or a string like this:
this is a long text=this is a very long text
"Robby" <ed****@not.my.email.com> wrote in message
news:u6**************@TK2MSFTNGP15.phx.gbl...
Find pattern
(?m)^(?([^[])(?<N1>\w+)|[[](?<N2>\w+=\w+)[]])=(?(N1)(?<V1>\w+)|[[](?<V2>\w+=\w+)[]])
Replace pattern
${N1}${N2} ControlChars.Tab ${V1}${V2}
The above find pattern only will work for pairs like ...
Name1=Value1
[Name2=CFODEV]=[Value2=CFO66]
To get it to work for all pairs change
=(?(N1) to =(?([^[]) therefore ...
(?m)^(?([^[])(?<N1>\w+)|[[](?<N2>\w+=\w+)[]])=(?([^[])(?<V1>\w+)|[[](?<V2>\w+=\w+)[]])
Then it will work for pairs like
Name1=Value1
[Name2=CFODEV]=[Value2=CFO66]
Name3=[Value3=DFO21]
[Name4=CFOESX]=Value4
Also, the find pattern will not work if you have spaces between the
tokens. However, you can easily modify it to account for spaces.
Robby
"Jéjé" <willgart_A_@hotmail_A_.com> wrote in message
news:el**************@TK2MSFTNGP14.phx.gbl... Hi,
I have a file which contain 1 pair of values by line like:
Name1=Value1
[Name2=CFODEV]=[Value2=CFO66]
I nned to store these pair of values in a sortedlist.
So the result expected for the 2 samples lines is:
Key Value
Name1 Value1
Name2=CFODEV Value2=CFO66
as you can see, when the name or the value contain a =, I can't use the
standard "split" method, but I use [ and ] as delimiters.
so I'm looking for a RegEx to read into the delimiters [ ]. and produce
2 results.
thanks for your help.
Jerome.