473,758 Members | 5,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1588

Find pattern

(?m)^(?([^[])(?<N1>\w+)|[[](?<N2>\w+=\w+)[]])=(?(N1)(?<V1>\ w+)|[[](?<V2>\w+=\w+)[]])

Replace pattern

${N1}${N2} ControlChars.Ta b ${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_@ho tmail_A_.com> wrote in message
news:el******** ******@TK2MSFTN GP14.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******** ******@TK2MSFTN GP15.phx.gbl...

Find pattern

(?m)^(?([^[])(?<N1>\w+)|[[](?<N2>\w+=\w+)[]])=(?(N1)(?<V1>\ w+)|[[](?<V2>\w+=\w+)[]])

Replace pattern

${N1}${N2} ControlChars.Ta b ${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_@ho tmail_A_.com> wrote in message
news:el******** ******@TK2MSFTN GP14.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.Co mpiled)

Const line1 As String = "Name1 = Value1"
Const line2 As String = "[Name2=CFODEV]=[Value2=CFO66]"
Const line3 As String = "Name1=Valu e1"
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_@ho tmail_A_.com> wrote in message
news:Ol******** ******@TK2MSFTN GP10.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******** ******@TK2MSFTN GP15.phx.gbl...

Find pattern

(?m)^(?([^[])(?<N1>\w+)|[[](?<N2>\w+=\w+)[]])=(?(N1)(?<V1>\ w+)|[[](?<V2>\w+=\w+)[]])

Replace pattern

${N1}${N2} ControlChars.Ta b ${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_@ho tmail_A_.com> wrote in message
news:el******** ******@TK2MSFTN GP14.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******** *****@TK2MSFTNG P12.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.Co mpiled)

Const line1 As String = "Name1 = Value1"
Const line2 As String = "[Name2=CFODEV]=[Value2=CFO66]"
Const line3 As String = "Name1=Valu e1"
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_@ho tmail_A_.com> wrote in message
news:Ol******** ******@TK2MSFTN GP10.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******** ******@TK2MSFTN GP15.phx.gbl...

Find pattern

(?m)^(?([^[])(?<N1>\w+)|[[](?<N2>\w+=\w+)[]])=(?(N1)(?<V1>\ w+)|[[](?<V2>\w+=\w+)[]])

Replace pattern

${N1}${N2} ControlChars.Ta b ${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_@ho tmail_A_.com> wrote in message
news:el******** ******@TK2MSFTN GP14.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
9768
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 numeric value according to an arbitrary regular expression.
9
4258
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). Why does this function replace the found reg ex. with the actual string "\t" and not a tab? (in the example below out of frustration I actually hardcoded the "\t") Private Shared Function replaceAll(ByVal strIn As String, ByVal strFind As...
7
2260
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 getting the results I expect. Dim SA as string() Dim S as string S="FBE"
4
3609
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 "LOT:". Source text may contain multiple "SUB: ... LOT:" blocks. For example this is my source text:
8
1906
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 there are numbers, then it should NOT have the letters. If it has the letters, then it should NOT have the numbers. My pattern for the letters is and numbers is the entire range . How can I specify matching one group or the other, and not both?
8
10289
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 bunch of hits with this - I'm only interested in the *last* one. Is there a way to build the concept "last" into the
2
4190
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 used/implimented I would appreciate it. Ideally I would like a vbscript over javascript so we can have it implimented on an access db as well. Thanks.
1
1867
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 subnet:((\d{1,3})|(\d{1,3}(\.\d{1,3}){1,3}))/(\d{1,2}) so I have list of ip/subnets strewn around like this
0
1543
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" java.lang.StackOverflowError at java.util.regex.Pattern$Branch.match(Pattern.java:4530) at java.util.regex.Pattern$GroupHead.match(Pattern.java:4570) I am not sure where in the regex is causing the inifinite loop. Can anyone shed light on this?
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.