473,727 Members | 1,988 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 1586

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
9761
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
4257
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
2259
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
3605
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
1905
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
4188
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
1865
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
1542
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
8891
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8103
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...
1
6703
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4786
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
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
2
2639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2158
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.