Link to home
Start Free TrialLog in
Avatar of kevp75
kevp75Flag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kevp75

ASKER

odd.  doesn't match anything for me in RegEx Coach
Some regex tools don't support lookahead and lookbehind
But it should be fine in .NET
Avatar of kevp75

ASKER

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

Try:
Dim re As Regex = New Regex(@"(?<=##)([^&\n]*?)(&.*?)?(?=##)")
Avatar of kevp75

ASKER

error.  'Expression Expected'
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]*?)(&.*?)?(?=##)")
@TerryAtOpus

"@" strings are C#-specific  : )


@kevp75

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

untitled.PNG
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>    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   : )
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.
Avatar of kevp75

ASKER

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
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:
>>  ps: Thanks kaufmed for the clarification regarding the @ symbol.

None required   = )


Come on Terry...  jump into VB programming. You know you want to  ; )
Avatar of kevp75

ASKER

;)  I need to do the reverse...eventually...  I will say this though...  I love XML Literals
>>   I will say this though...  I love XML Literals

Indeed. They are pretty nifty   = )
Avatar of kevp75

ASKER

can we reduce the points?   I'll admit he helped, however it didn't end up being the solution?
Avatar of kevp75

ASKER

:)  yeah.... that was a DOH! moment ;)
Avatar of kevp75

ASKER

ultimate solution