MK01111000
MK01111000

Reputation: 832

How do I match subscript with a regular expression in VBA

Among other strings, I am trying to match the string LC₅₀
In VBA I am able to type superscript characters but not subscript characters. I have the following regular expression

Dim r1 As Object
Set r1 = New RegExp
r1.Pattern = "^(\(?[a-z].*[a-z]\)?\.?,?-?:?|\(?\d{1,5}|%,?|[a-z]|LC50|\D.*³|[A-Z]\d{1},?|\(?\d{1,4}\..*\)?|°.*|[A-Z]\d(-|:).*|MPa|\(o-,|m-,|p-\))$": r1.Global = True: Set r1Matches = r1.Execute(Replace(arry(i), "- ", "-"))

My question is: How do I match the following standalone string with a regular expression in VBA
LC₅₀

Upvotes: 1

Views: 74

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627180

You can replace LC50 with LC[5\u2085][0\u2080].

With some other small enhancements, the pattern can look like

^(\(?[a-z].*[a-z]\)?\.?,?-?:?|\(?\d{1,5}|%,?|[a-z]|LC[5\u2085][0\u2080]|\D.*³|[A-Z]\d,?|\(?\d{1,4}\..*\)?|°.*|[A-Z]\d[-:].*|MPa|\([om]-,|p-\))$

Here,

  • \(o-,|\(m-, is replaced with \([om]-,
  • [A-Z]\d(-|:).* replaced with [A-Z]\d[-:].*
  • The {1} is removed, as it is always redundant.

The subscript number codes are

Number Code
\u2080
\u2081
\u2082
\u2083
\u2084
\u2085
\u2086
\u2087
\u2088
\u2089

Upvotes: 1

Related Questions