Reputation: 33
I am trying to replace a portion of a text that is between apostrophes, not all, just a part. For example, I need to replace the characters /* and */ that are only within text between quotes by null text but not outside them.
My input text, for example:
A = 'THIS IS AN ALPHABETIC /* CONSTANT' || WS_CON1 /* */ || 'TEST STRING */';
Expected output:
A = 'THIS IS AN ALPHABETIC CONSTANT' || WS_CON1 /* */ || 'TEST STRING ';
I extracted the texts in quotes but I do not know how to replace the /* and */ with null text.
Sub ReplaceWithRegex() Dim strPattern As String Dim strReplace As String Dim regEx As Variant Dim strtxt As String Set regEx = CreateObject("vbscript.regexp") strtxt = "A = 'THIS IS AN ALPHABETIC /* CONSTANT' || WS_CON1 /* */ || ' TEST STRING */';" strPattern = "\'([^\']*)\'" strReplace = "" With regEx .Global = True .MultiLine = True .IgnoreCase = False .Pattern = strPattern End With If regEx.Test(strtxt) Then Debug.Print regEx.Replace(strtxt, strReplace) Else MsgBox ("Not matched") End If End Sub
Obviously, this replace all text between quotes to null string.
How do I solve this problem?
Upvotes: 1
Views: 1530
Reputation: 27723
This expression might help you to replace those undesired /*
:
[A-Z]\s\/\*\s[A-Z]
We can simply wrap that in a capturing groups ()
, similar to:
([A-Z])\s(\/\*)\s([A-Z])
Then, we can replace it with $1 $3
and ignore the second undesired capturing group:
This tool helps you to modify/change/edit your expressions, as you wish.
This link helps you to visualize your expressions:
If you may have more patterns in these capturing groups, you can simply add them using an |, such as:
([A-Z])\s(\/\*|\*\/)(\s[A-Z]|\x27)
You might also want to use \x27
instead of '
such that your code would become easy to read.
const regex = /([A-Z])\s(\/\*|\*\/)(\s[A-Z]|\x27)/gm;
const str = `A = 'THIS IS AN ALPHABETIC /* CONSTANT\' || WS_CON1 /* */ || \'TEST STRING */';`;
const subst = `$1$3`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
This snippet returns the runtime of a 1-million times for
loop.
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const regex = /([A-Z])\s(\/\*|\*\/)(\s[A-Z]|\x27)/gm;
const str = `A = 'THIS IS AN ALPHABETIC /* CONSTANT\' || WS_CON1 /* */ || \'TEST STRING */';`;
const subst = `$1$3`;
var match = str.replace(regex, subst);
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");
Upvotes: 7
Reputation: 60174
Here is another VBA method using Regular Expressions.
Option Explicit
'Set Reference to Microsoft VBScript Regular Expressions 5.5
Function reReplaceComment(S As String) As String
Dim RE As RegExp
Dim I As Long, J As Long
Set RE = New RegExp
With RE
.Global = True
.Pattern = "('[^']*?)(?:(?:/\*)|(?:\*/))([^']*?')"
reReplaceComment = .Replace(S, "$1$2")
End With
End Function
('[^']*?)(?:(?:/\*)|(?:\*/))([^']*?')
('[^']*?)
(?:(?:/\*)|(?:\*/))
([^']*?')
$1$2
$1
$2
Created with RegexBuddy
Upvotes: 3