为乘法的数字添加双引号"

huangapple go评论77阅读模式
英文:

Add double quotations " for the multiplied numbers

问题

I need to add double quotations " for the multiplied numbers for the given strings.
The multiplication operator (as a string) is either "x", "X" or "*".
I have used the below function to extract the multiplied numbers, originated question Here.

Current String Expected Result
string 2 x 3 string string 2"x3" string
string 2X3" string string 2"X3" string
string 2*3*5 string string 2"*3"*5" string
string 2"*3 string string 2"*3" string
string 4.5 x 5 string string 4.5"x5" string
string 4"x5" string string 4"x5" string
string 4x 3.5"  string string 4"x3.5" string

Option Explicit
Option Compare Text

Function RegexExtractt(str, Optional pos As Integer = 0) As String

Static re As Object
If re Is Nothing Then Set re = CreateObject("vbscript.regexp")

re.Pattern = "\d+(?:\.\d+)?(?:''|""|in(?:ch)?\b)?(?:\s*[*x]\s*\d+(?:\.\d+)?(?:''|""|in(?:ch)?\b)?)*\s*[*x]\s*\d+(?:\.\d+)?"
re.Global = False
re.IgnoreCase = True

If re.test(str) Then
    RegexExtractt = re.Execute(str)(pos)
Else
    RegexExtractt = vbNullString
End If

End Function

英文:

I need to add double quotations " for the multiplied numbers for the given strings.
The multiplication operator (as a string) is either "x" , "X" or "*".
I have used the below function to extract the multiplied numbers, originated question Here.

Current String Expected Result
string 2 x 3 string string 2"x3" string
string 2X3" string string 2"X3" string
string 2*3*5 string string 2"*3"*5" string
string 2"*3 string string 2"*3" string
string 4.5 x 5 string string 4.5"x5" string
string 4"x5" string string 4"x5" string
string 4x 3.5"  string string 4"x3.5" string
Option Explicit
Option Compare Text

    Function RegexExtractt(str, Optional pos As Integer = 0) As String
    
        Static re As Object
        If re Is Nothing Then Set re = CreateObject("vbscript.regexp")
        
        re.Pattern = "\d+(?:\.\d+)?(?:''|""|in(?:ch)?\b)?(?:\s*[*x]\s*\d+(?:\.\d+)?(?:''|""|in(?:ch)?\b)?)*\s*[*x]\s*\d+(?:\.\d+)?"
        re.Global = False
        re.IgnoreCase = True
        
        If re.test(str) Then
            RegexExtractt = re.Execute(str)(pos)
        Else
            RegexExtractt = vbNullString
        End If
    
    End Function 

答案1

得分: 3

If you are ok with doing a second replace for "" to " after obtaining for example this result string 2"*3""*5" string you could first do:

"?\s*([xX*])\s*(\d+(?:\s*\.\d+)?)"?

The pattern matches

  • "?\s* 匹配可选的 " 和可选的空白字符
  • ([xX*]) 捕获 x X * 中的一个在 第一组
  • \s* 匹配可选的空白字符
  • (\d+(?:\s*\.\d+)?)第二组 中捕获1个或多个数字,带有可选的小数部分
  • "? 可选匹配 "

在第一次替换中使用双引号括起来的2个捕获组 "$1$2"

查看正则表达式演示

英文:

If you are ok with doing a second replace for "" to " after obtaining for example this result string 2"*3""*5" string you could first do:

"?\s*([xX*])\s*(\d+(?:\s*\.\d+)?)"?

The pattern matches

  • "?\s* Match an optional " and optional whitespace chars
  • ([xX*]) Capture one of x X * in group 1
  • \s* Match optional whitespace chars
  • (\d+(?:\s*\.\d+)?) Capture 1+ digits with an optional decimal part in group 2
  • "? Optionally match "

In the first replacement use the 2 capture groups between double quotes "$1$2"

See a regex demo.

答案2

得分: 2

以下是要翻译的内容:

根据您的先前问题,这是我的建议:

(\d+(?:.\d+)?)(?:''|"|in(?:ch)?\b)?(?:\s*([x])\s(?=\d+(?:.\d+)?))?

替换为:

$1"$2

请参阅在线演示。


通过以下方式调用:

=RegexReplace(A1,"(\d+(?:.\d+)?)(?:''|""|in(?:ch)?\b)?(?:\s*([x])\s(?=\d+(?:\

英文:

Based on your previous questions, this is my two cents:

(\d+(?:\.\d+)?)(?:''|"|in(?:ch)?\b)?(?:\s*([*x])\s*(?=\d+(?:\.\d+)?))?

Replace with:

$1"$2

See this online demo.


Invoke this through:

=RegexReplace(A1,"(\d+(?:\.\d+)?)(?:''|""|in(?:ch)?\b)?(?:\s*([*x])\s*(?=\d+(?:\.\d+)?))?","$1""$2")

Note: This is based on the replace-function as per your linked previous question, which is case-insensitive per design.

Also, maybe your sample data is a bit too simplified. I can't imagine this to be working on actual data which may contain digits in other places you want to leave untouched.

答案3

得分: 1

以下是已经翻译好的内容:

你可以使用以下的VBA函数,该函数涵盖了示例的所有情况:
(有一个注意事项:事情并不总是如表面看起来的…存在一个不可打印的字符,其代码为160,它看起来像一个空格-代码32-但它实际上不是。)

'如果使用Option Compare Text,则此代码不起作用,因为存在x和X
Public Function doTheJob(s As String) As String
   Const qt = """", m0 = " ", m1 = "x", m2 = "X", m3 = "*"
   Dim t As String, pa As Integer, pz As Integer
   t = Replace(s, m0, " ")
   pa = InStr(1, t, " ")
   pz = InStrRev(t, " ")
   doTheJob = Left(t, pa) & Replace(Replace(Replace(Replace(Replace(Trim(Mid(t, pa + 1, pz - pa)), qt, "") _
                  , " ", ""), m1, qt & m1), m2, qt & m2), m3, qt & m3) & qt & Mid(t, pz)
End Function

'以下的代码适用于有或没有使用Option Compare Text
Public Function doTheJob(s As String) As String
   Const qt = """", m0 = " ", m1 = "x", m2 = "X", m3 = "*"
   Dim t As String, pa As Integer, pz As Integer
   t = Replace(s, m0, " ")
   pa = InStr(1, t, " ")
   pz = InStrRev(t, " ")
   t = Replace(Replace(Trim(Mid(t, pa + 1, pz - pa)), qt, ""), " ", "")
   If InStr(1, t, m3) Then
      t = Replace(t, m3, qt & m3)
   Else
      If InStr(1, t, m1) Then
         t = Replace(t, m1, qt & m1)
      Else
         t = Replace(t, m2, qt & m2)
      End If
   End If
   doTheJob = Left(s, pa) & t & qt & Mid(s, pz)
End Function

请注意,这是给定代码的翻译,不包括任何其他内容。

英文:

You can use the following VBA function, which covers all cases of the example:
(with a caveat: Things are not always as they seem... there is the unprintable character with code 160, it looks like a space -code 32- but it's not.)

'The code don't work if use Option Compare Text because of x and X
Public Function doTheJob(s As String) As String
   Const qt = """", m0 = " ", m1 = "x", m2 = "X", m3 = "*"
   Dim t As String, pa As Integer, pz As Integer
   t = Replace(s, m0, " ")
   pa = InStr(1, t, " ")
   pz = InStrRev(t, " ")
   doTheJob = Left(t, pa) & Replace(Replace(Replace(Replace(Replace(Trim(Mid(t, pa + 1, pz - pa)), qt, "") _
                  , " ", ""), m1, qt & m1), m2, qt & m2), m3, qt & m3) & qt & Mid(t, pz)
End Function
 

'The code below works WITH or WITHOUT Option Compare Text
Public Function doTheJob(s As String) As String
   Const qt = """", m0 = " ", m1 = "x", m2 = "X", m3 = "*"
   Dim t As String, pa As Integer, pz As Integer
   t = Replace(s, m0, " ")
   pa = InStr(1, t, " ")
   pz = InStrRev(t, " ")
   t = Replace(Replace(Trim(Mid(t, pa + 1, pz - pa)), qt, ""), " ", "")
   If InStr(1, t, m3) Then
      t = Replace(t, m3, qt & m3)
   Else
      If InStr(1, t, m1) Then
         t = Replace(t, m1, qt & m1)
      Else
         t = Replace(t, m2, qt & m2)
      End If
   End If
   doTheJob = Left(s, pa) & t & qt & Mid(s, pz)
End Function

为乘法的数字添加双引号"

huangapple
  • 本文由 发表于 2023年5月7日 15:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定