英文:
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 ofx
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论