473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

looking for a RegEx pattern to do this...

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.
Nov 21 '05 #1
4 1557

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.

Nov 21 '05 #2
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.


Nov 21 '05 #3
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.



Nov 21 '05 #4
thanks a lot !!!
this code works fine.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eB*************@TK2MSFTNGP12.phx.gbl...
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.



Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
9
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression)....
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
8
by: tomb | last post by:
I have found much documentation on specific patterns, but I can't find anything explaining how to reject strings that match both groups. My string could contain specific letters or numbers. If...
8
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
2
by: Rob | last post by:
I'm looking for a mod 10 script that you know to work well. I have googled and found a few different ones but I would like a 2nd opinion. If you can please link me to a mod 10 script that you have...
1
by: Prabhu Gurumurthy | last post by:
Hello all - I have a file which has IP address and subnet number and I use regex to extract the IP separately from subnet. pattern used for IP: \d{1,3}(\.\d{1,3}){3} pattern used for...
0
by: vmysore | last post by:
I am trying to get all the columns selected within a SQL query (including the sub selects). When the code hits matcher.find(). i get the following exception: Exception in thread "main"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.