作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习正则表达式来回答有关葡萄牙语的问题。
.MultiLine = False
)? 1 One without dot. 2. Some Random String. 3.1 With SubItens. 3.2 With number 0n mid. 4. Number 9 incorrect. 11.12 More than one digit. 12.7 Ending (no word).
1 One without dot.
2. Some Random String.
3.1 With SubItens.
3.2 With number 0n mid.
4. Number 9 incorrect.
11.12 More than one digit.
12.7 Ending (no word).
我想的是使用Regex with Split ,但我无法在 Excel 上实现该示例。
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "plum-pear"
Dim pattern As String = "(-)"
Dim substrings() As String = Regex.Split(input, pattern) ' Split on hyphens.
For Each match As String In substrings
Console.WriteLine("'{0}'", match)
Next
End Sub
End Module
' The method writes the following to the console:
' 'plum'
' '-'
' 'pear'
正在阅读this和 this 。 RegExr Website与表达式 /([0-9]{1,2})([.]{0,1})([0-9]{0,2})/igm
一起使用输入。
得到以下结果:
有更好的方法吗?正则表达式是正确的还是更好的生成方式?我在 google 上找到的示例并没有启发我如何正确使用 RegEx 和 Split。
也许我对 Split 函数的逻辑感到困惑,我想获取分割索引,而分隔符字符串是正则表达式。
最佳答案
I can make that it ends with word and period
使用
\d+(?:\.\d+)*[\s\S]*?\w+\.
请参阅regex demo .
详细信息
\d+
- 1 位或更多数字(?:\.\d+)*
- 零个或多个序列:
\.
- 点\d+
- 1 位或更多数字[\s\S]*?
- 任何 0+ 个字符,尽可能少,直到第一个...\w+\.
- 1 个以上单词字符,后跟 .
.这里是一个示例 VBA 代码:
Dim str As String
Dim objMatches As Object
str = " 1 One without dot. 2. Some Random String. 3.1 With SubItens. 3.2 With Another SubItem. 4. List item. 11.12 More than one digit."
Set objRegExp = New regexp ' CreateObject("VBScript.RegExp")
objRegExp.Pattern = "\d+(?:\.\d+)*[\s\S]*?\w+\."
objRegExp.Global = True
Set objMatches = objRegExp.Execute(str)
If objMatches.Count <> 0 Then
For Each m In objMatches
Debug.Print m.Value
Next
End If
注意
您可能要求匹配仅停在单词 + .
处,后跟 0+ 个空格和一个使用 \d+(?:\.\d+)*[\s\S]*?[a-zA-Z]+\.(?=\s*(?:\d+|$))
的数字。 .
(?=\s*(?:\d+|$))
正向先行要求存在 0+ 个空格 (\s*
),后跟 1 + 数字 (\d+
) 或字符串结尾 ($
) 紧邻当前位置的右侧。
关于regex - 使用正则表达式将编号列表数组拆分为多行编号列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46331543/
我是一名优秀的程序员,十分优秀!