需要一些帮助来编写正则表达式来计算字符串中的单词数量(请注意,数据是 html字符串,需要将其放入电子表格)由任何特殊字符分隔,例如。, - , + , / ,标签等。计数应排除特殊字符。
**Original String** **End Result**
Ex : One -> 1
One. -> 1
One Two -> 2
One.Two -> 2
One Two. -> 2
One.Two. -> 2
One.Tw.o -> 3
答案 0 :(得分:0)
尝试使用此代码,所有必要的注释都在代码中:
Sub SpecialSplit()
Dim i As Long
Dim str As String
Dim arr() As String
Dim delimeters() As String
'here you define all special delimeters you want to use
delimetres = Array(".", "+", "-", "/")
For i = 1 To 9
str = Cells(i, 1).Value
'this will protect us from situation where last character is delimeter and we have additional empty string
str = Left(str, Len(str) - 1)
'here we replace all special delimeters with space to simplify
For Each delimeter In delimetres
str = Replace(str, delimeter, " ")
Next
arr = Split(str)
Cells(i, 2).Value = UBound(arr) - LBound(arr) + 1
Next
End Sub
答案 1 :(得分:0)
使用RegExp后发布的数据工作正常。将它放在Visual Basic编辑器中的通用模块中。
Public Function CountWords(strInput As String) As Long
Dim objMatches
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.Pattern = "\w+"
Set objMatches = .Execute(strInput)
CountWords = objMatches.Count
End With
End Function
你必须像普通公式一样使用它。例如假设数据在单元格A1函数中将是:
=CountWords(A1)
对于您的信息,如果字符数特定,则可以通过公式实现:
=LEN(TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TRIM(A1),"."," "),","," "),"-"," "),"+"," "),"/"," "),"\"," ")))-LEN(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TRIM(A1),"."," "),","," "),"-"," "),"+"," "),"/"," "),"\"," "))," ",""))+1
答案 2 :(得分:0)
<强>更新强>
我认为你提出了一个有价值的问题,这种贬低是不公平的!
Function WCount(ByVal strWrd As String) As Long
'Variable declaration
Dim Delimiters() As Variant
Dim Delimiter As Variant
'Initialization
Delimiters = Array("+", "-", ".", "/", Chr(13), Chr(9)) 'Define your delimiter characters here.
'Core
For Each Delimiter In Delimiters
strWrd = Replace(strWrd, Delimiter, " ")
Next Delimiter
strWrd = Trim(strWrd)
Do While InStr(1, strWrd, " ") > 0
strWrd = Replace(strWrd, " ", " ")
Loop
WCount = UBound(Split(strWrd, " ")) + 1
End Function
<强> ________________ 强>
您可以在excel公式中将此功能用作UDF
,也可以在其他VBA
代码中使用。
=WCOUNT("One.Two.Three.")
或=WCOUNT($A$1")
。
(假设用Str
参数传递你的字符串。)
Sub test()
Debug.Print WCount(Str)
End Sub
问候。
我测试了你的文字,如下所示。
将文本复制到Excel单元格中,如图所示。
更新了Line break
和Tab
个字符的代码,并正确计算您的字符串字数。