Help with RegEx, please.

I have some ugly text that I need to process:

{n]UUID[/n}{v]rec4[/v}{n]ModTS[/n}{v][/v}{n]ModUser[/n}{v][/v}{n]NameFirst[/n}{v]Pepper[/v}{n]NameLast[/n}{v]Potts![/v}{n]NameCompany[/n}{v]Stark Industries[/v}{n]Notes[/n}{v]Assistant to Tony[/v}

I’d like to create two strings from this using Kem’s awesome m_string module. Field names are wrapped in “{n][/n}” for field names and “{v][/v}” for field values.

Trying to get all the names by removing all the values.

theFieldValuesNames = m_string.ReplaceRegEx( theFieldValuesNames, "{v\\].*\\[/v}", "" )

Which only returns the first field name: "{n]UUID[/n} "

Trying to get all the values by removing all the names.

theFieldValuesValues = m_string.ReplaceRegEx( theFieldValuesValues, "{n\\].*\\[/n}", "" )

Which only returns the last field value: “{v]Assistant to Tony[/v}”

Any ideas? I kinda get grep, but not very well… :frowning:

RegEx is voodoo in my opinion… :slight_smile:

converted to xml format which might help:

[code] dim sourceStr as String = “{n]UUID[/n}{v]rec4[/v}{n]ModTS[/n}{v][/v}{n]ModUser[/n}{v][/v}{n]NameFirst[/n}{v]Pepper[/v}{n]NameLast[/n}{v]Potts![/v}{n]NameCompany[/n}{v]Stark Industries[/v}{n]Notes[/n}{v]Assistant to Tony[/v}”

dim modifyStr as String = “” + sourceStr.ReplaceAll("{n]", “”).ReplaceAll("[/n}{v]", “”).ReplaceAll("[/v}", “”) + “”
[/code]

Thanks Rich. I created a while loop to get do the cleanup, but I’d love to understand how to use grep…

Grep is the command line tool
Regex is probably what you mean … no ?

The problem is greediness. In this case you have to turn it off.

Think of it this way. Let’s say you want all the text between the “@” symbol, so you might write a pattern like this: @.*@. All well and good if your string is “@field@”, but what if it’s “@field1@ some text @field2@”? What you’ll match by default is “@field1@ some text @field2@” instead of just “@field1”. Why? Because the pattern was told to be “greedy”, that is, match as much as it can.

Looking at it another way, you are saying, “start looking for the first @ from the start of the string, then look for the last @ from the back of the string.”

By turning off Greedy, you tell it to stop as soon as it has a complete match. You can do that by either filling in the greedy parameter on my ReplaceRegEx method, or by prefixing your pattern with code[/code]. The latter is more portable.

(The “U” stands for “Ungreedy”, which is an odd way to do it, but it is what it is.)

Yes… Thank you Norm. I mean RegEx.

Thanks Kem! You made that easy to understand!

Thanks again Kem! Worked perfectly. Just had to add false for greedy!

theFieldValuesNames = m_string.ReplaceRegEx( theFieldValuesNames, "{v\\].*\\[/v}", "", false ) theFieldValuesValues = m_string.ReplaceRegEx( theFieldValuesValues, "{n\\].*\\[/n}", "", false )

nice & regular
replaceall and split would suffice
not sure how that compares to a regex

dim s as string =
dim lines() as string

lines = s.ReplaceAll("{n]", “”).ReplaceAll("[/n}{v]", “=”).ReplaceAll("[/v}", EndOfLIne).Trim.Split(EndOfLine)

Thanks for that Norm. I did something similar effectively looping til all the strings I needed to remove were gone… The RegEx code is so much tighter though…

// Remove the Values from Names and Names from Values without RegEx while theFieldValuesNames.InStr( app.AuditLogFieldValueBegin ) > 0 theStringToRemoveBegin = theFieldValuesNames.InStr( app.AuditLogFieldValueBegin ) theStringToRemoveEnd = theFieldValuesNames.InStr( theStringToRemoveBegin, app.AuditLogFieldValueEnd ) theStringToRemove = theFieldValuesNames.mid( theStringToRemoveBegin, theStringToRemoveEnd + len( app.AuditLogFieldValueEnd ) - theStringToRemoveBegin ) theFieldValuesNames = theFieldValuesNames.replaceall( theStringToRemove, "" ) wend while theFieldValuesValues.InStr( app.AuditLogFieldNameBegin ) > 0 theStringToRemoveBegin = theFieldValuesValues.InStr( app.AuditLogFieldNameBegin ) theStringToRemoveEnd = theFieldValuesValues.InStr( theStringToRemoveBegin, app.AuditLogFieldNameEnd ) theStringToRemove = theFieldValuesValues.mid( theStringToRemoveBegin, theStringToRemoveEnd + len( app.AuditLogFieldNameEnd ) - theStringToRemoveBegin ) theFieldValuesValues = theFieldValuesValues.replaceall( theStringToRemove, "" ) wend

Just renamed the thread. Changed ‘Grep’ to ‘RegEx’.

huh
my one liner vs that long gawdawful regex loop :stuck_out_tongue:

ducks since kem will no doubt smack me for dissing regex :slight_smile:

  1. That’s not a RegEx loop, it’s one for avoiding RegEx.
  2. Calling yours a “one-liner” is cheating a bit, no? :slight_smile:

[quote=226195:@Kem Tekinay]

  1. Calling yours a “one-liner” is cheating a bit, no? :)[/quote]
    How its one line of chained methods calls … one line of code

Maybe I caused the confusion. The one liners I posted were using RegEx. The loop mess is what I wrote til Kem fixed my RegEx problem…