Little RegEx Help

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

LVL 25
kevp75Asked:
Who is Participating?
 
Terry WoodsConnect With a Mentor IT GuruCommented:
This pattern seems to work for me:
(?<=##)([^&\n]*?)(&.*?)?(?=##)

Tested here:
http://www.myregextester.com/?r=f2d4d15f
0
 
kevp75Author Commented:
odd.  doesn't match anything for me in RegEx Coach
0
 
Terry WoodsIT GuruCommented:
Some regex tools don't support lookahead and lookbehind
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Terry WoodsIT GuruCommented:
But it should be fine in .NET
0
 
kevp75Author Commented:
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
 
Terry WoodsIT GuruCommented:
Try:
Dim re As Regex = New Regex(@"(?<=##)([^&\n]*?)(&.*?)?(?=##)")
0
 
kevp75Author Commented:
error.  'Expression Expected'
0
 
Terry WoodsIT GuruCommented:
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
 
käµfm³d 👽Commented:
@TerryAtOpus

"@" strings are C#-specific  : )


@kevp75

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

untitled.PNG
0
 
kevp75Connect With a Mentor Author Commented:
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
 
käµfm³d 👽Commented:
>>    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
 
Terry WoodsIT GuruCommented:
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
 
kevp75Author Commented:
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
 
Terry WoodsIT GuruCommented:
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
 
käµfm³d 👽Commented:
>>  ps: Thanks kaufmed for the clarification regarding the @ symbol.

None required   = )


Come on Terry...  jump into VB programming. You know you want to  ; )
0
 
kevp75Author Commented:
;)  I need to do the reverse...eventually...  I will say this though...  I love XML Literals
0
 
käµfm³d 👽Commented:
>>   I will say this though...  I love XML Literals

Indeed. They are pretty nifty   = )
0
 
kevp75Author Commented:
can we reduce the points?   I'll admit he helped, however it didn't end up being the solution?
0
 
kevp75Author Commented:
:)  yeah.... that was a DOH! moment ;)
0
 
kevp75Author Commented:
ultimate solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.