Question

I am using VBA to filter records in an MS Access Database I first need to clean the data in a particular column

An example item in this column would be XXXZZZSTYS1234NCYORGxxxASD

What i would like to do is if the string contains STY then remove subsequent characters until you reach COYORGxxx or NCYORGxxx or CNYORGxxx and then remove any characters after the ORGxxx if they exist

So in the example above it would be cleaned to XXXZZZSTYNCYORGxxx

It doesn't have to be VBA, I use notepad++ to test it

Was it helpful?

Solution

You could try the following regex:

(.*?STY).*?((COY|NCY|CNY)ORGxxx).*

And replace it with:

$1$2

Where:

  • $1 represents all characters until the first "STY"
  • $2 represents COYORGxxx or NCYORGxxx or CNYORGxxx

OTHER TIPS

There you go.

(.*STY).*((CO|NC|CN)YORGxxx).*
  • (.*STY) will catch every string of characters up to STY

  • .* will skip characters until next catch

  • ((CO|NC|CN)YORGxxx) will catch any string starting with CO, NC or CN followed by YORGxxx

  • and the last .* is just here to ensure the match (not necessary in this specific example)

You then only need to replace with $1$2, $n representing the n-th catch of the regex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top