Solved

Little RegEx Help

Posted on 2011-03-14
22
274 Views
Last Modified: 2012-05-11
Need a little help with the code search snippet that follows:

I need to break out the parts of the tokens, and I am having a hard time getting the regex right

WHat I need is:

TEST1 = Match Group 1
&ID=123&bob=yahoo&email=oioiuji = Match Group 2

for each of the ##...## passed in the string.


Here's what I got:
\#\#(.*?)\#\#

and it gets me the entire ##...##
<div>
<h1>##TEST1&ID=123&bob=yahoo&email=oioiujij##</h1>
<h1>##TEST2##</h1>
<h1>##TEST3##</h1>
<h1>##TEST4&ID=123&bob=yahoo&email=oioiujij##</h1>
<h1>##TEST5##</h1>
</div>

Open in new window

0
Comment
Question by:kevp75
  • 9
  • 7
  • 4
22 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 35132737
This pattern seems to work for me:
(?<=##)([^&\n]*?)(&.*?)?(?=##)

Tested here:
http://www.myregextester.com/?r=f2d4d15f
0
 
LVL 25

Author Comment

by:kevp75
ID: 35132777
odd.  doesn't match anything for me in RegEx Coach
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35132810
Some regex tools don't support lookahead and lookbehind
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Terry Woods
ID: 35132835
But it should be fine in .NET
0
 
LVL 25

Author Comment

by:kevp75
ID: 35132928
nah, just tried it. on a page, and it doesn't work.

Using a slightly modified code from that site:
            Dim tmpStr As New StringBuilder
            Dim sourcestring As String = _Template
            Dim re As Regex = New Regex("(?<=##)([^&\n]*?)(&.*?)?(?=##)")
            Dim mc As MatchCollection = re.Matches(sourcestring)
            Dim mIdx As Integer = 0
            For Each m As Match In mc
                For groupIdx As Integer = 0 To m.Groups.Count - 1
                    tmpStr.Append(mIdx & ":" & re.GetGroupNames(groupIdx) & "=" & m.Groups(groupIdx).Value & "<br />")
                Next
                mIdx = mIdx + 1
            Next
            Return tmpStr.ToString

returns me:
 
0:0=TEST1&ID=123&bob=yahoo&email=oioiujij<br />0:1=TEST1<br />0:2=&ID=123&bob=yahoo&email=oioiujij<br />1:0=</h1>    <h1><br />1:1=</h1>    <h1><br />1:2=<br />2:0=TEST2<br />2:1=TEST2<br />2:2=<br />3:0=</h1>    <h1><br />3:1=</h1>    <h1><br />3:2=<br />4:0=TEST3<br />4:1=TEST3<br />4:2=<br />5:0=</h1>    <h1><br />5:1=</h1>    <h1><br />5:2=<br />6:0=TEST4&ID=123&bob=yahoo&email=oioiujij<br />6:1=TEST4<br />6:2=&ID=123&bob=yahoo&email=oioiujij<br />7:0=</h1>    <h1><br />7:1=</h1>    <h1><br />7:2=<br />8:0=TEST5<br />8:1=TEST5<br />8:2=<br />

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35133067
Try:
Dim re As Regex = New Regex(@"(?<=##)([^&\n]*?)(&.*?)?(?=##)")
0
 
LVL 25

Author Comment

by:kevp75
ID: 35133118
error.  'Expression Expected'
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35133129
Hmmm... I'm struggling a little with the syntax, because I don't program in that language.

You could try doubling the backslash instead:
Dim re As Regex = New Regex("(?<=##)([^&\\n]*?)(&.*?)?(?=##)")
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35137385
@TerryAtOpus

"@" strings are C#-specific  : )


@kevp75

Here's the output of TerryAtOpus' pattern in Expresso, a .NET-capable regex utility:

untitled.PNG
0
 
LVL 25

Assisted Solution

by:kevp75
kevp75 earned 0 total points
ID: 35137546
aye.  I understand it works in your software.  the issue I am having is it does not work in a vb.net web page.

In fact, I have found that this pattern does work:
Dim _TokenRE As New Regex("\#\#([^&\n]*?)(&.*?)?\#\#")
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35137594
>>    I understand it works in your software...

The point is that if it works in our software, it should work in your page because both are using the same regex engine.

The backslashes on the hash symbols should not be needed.

Glad you found a solution   : )
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35142173
The pattern for the solution found by the asker:
"\#\#([^&\n]*?)(&.*?)?\#\#"
is heavily based on my solution:
(?<=##)([^&\n]*?)(&.*?)?(?=##)
therefore at least some points should be awarded.

This is because the core pattern of my pattern has been used:
([^&\n]*?)(&.*?)?

ps: Thanks kaufmed for the clarification regarding the @ symbol.
0
 
LVL 25

Author Comment

by:kevp75
ID: 35142291
but you didn't answer the question, so I guess it will be dependant on what the moderators feel would be fair.

@Mods, I would agree to a point reduction...but definately not full points
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35142375
To clarify:

There is a typo here:
This is because the core pattern of my pattern has been used
It should read:
This is because the core part of my pattern has been used:
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35148659
>>  ps: Thanks kaufmed for the clarification regarding the @ symbol.

None required   = )


Come on Terry...  jump into VB programming. You know you want to  ; )
0
 
LVL 25

Author Comment

by:kevp75
ID: 35148880
;)  I need to do the reverse...eventually...  I will say this though...  I love XML Literals
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35149328
>>   I will say this though...  I love XML Literals

Indeed. They are pretty nifty   = )
0
 
LVL 25

Author Comment

by:kevp75
ID: 35168382
can we reduce the points?   I'll admit he helped, however it didn't end up being the solution?
0
 
LVL 25

Author Comment

by:kevp75
ID: 35168735
:)  yeah.... that was a DOH! moment ;)
0
 
LVL 25

Author Closing Comment

by:kevp75
ID: 35196724
ultimate solution
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question