1

I have a few strings that look like the following:

xml=<data><optional><Account>192</Account></optional></data>, submitter=Q102, target=Q10, escalationType=esc, escalationReason=277, feedback=cx req live esc to have us review, adv cx his account is pending, preventable=0, 

What I am trying to do is write a regular expression in excel (vb) to find all the text between feedback= and , preventable and replace all , in that with a PIPE instead.

This is the expression I have so far to find that text:

(?<=feedback=)(.*)(?=, preventable)/gi

However, I'm not too sure what to do at this point to replace text within that selection?

Here is the expected string after it has ran:

 xml=<data><optional><Account>192</Account></optional></data>, submitter=Q102, target=Q10, escalationType=esc, escalationReason=277, feedback=cx req live esc to have us review| adv cx his account is pending, preventable=0, 

Here is a link to the Regex I am working with in my test: https://regex101.com/r/wR1dL0/2

4
  • If there will always have two phrases, then it could help: regex101.com/r/wR1dL0/3 Commented Jul 16, 2015 at 16:28
  • Sadly it could be a paragraph long with commas in any part of it so I'm not sure if its even possible with one epression Commented Jul 16, 2015 at 16:29
  • You probably want to use non-greedy .+? there. But, normally when you want to do two separate things in a single replace function, you would use a callback functionality. For example, in the replacement portion, specify a function that returns a replacement. In that function, take all from capture group 1, replace , -> |, then return the result. Commented Jul 16, 2015 at 16:39
  • A problem with your regex: VBScript Regular Expressions does not support look behind or mode modifiers Commented Jul 16, 2015 at 18:06

1 Answer 1

2

At regex101 you have selected the PCRE flavor, but if you want to use VBA, you need to select the javascript flavor. In particular, look-behinds are not valid; nor are mode modifiers, in vbscript (javascript).

A regex that will match as you require would be:

,(?!.*feedback)(?=.*,\s*preventable)

The replacement string would merely be the PIPE

A UDF implementing that in VBA might be:


Option Explicit
Function CommaToPipeBetween(S As String, StartAt As String, EndAt As String) As String
    Dim RE As Object
    Dim sPat As String

sPat = ",(?!.*" & StartAt & ")(?=.*,\s*" & EndAt & ")"

Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = True
    .Pattern = sPat
    .ignorecase = True

    CommaToPipeBetween = .Replace(S, "|")

End With

End Function

The UDF assumes that you do not want to replace a comma just before EndAt if it is separated by 0 or more space characters. But you could change the pattern to make things more generalized, if you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.