【发布时间】:2018-03-02 01:47:57
【问题描述】:
我正在尝试学习正则表达式来回答关于 SO 葡萄牙语的问题。
输入(单元格上的数组或字符串,所以.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。
也许我对拆分函数的逻辑感到困惑,我想获得拆分索引,而分隔符字符串是正则表达式。
【问题讨论】:
-
寻找 String.Replace(regex) - 和谷歌 BackReferences。我认为会类似于
input.Replace("([0-9]*\.?[0-9]*)", "\0" + vbcrlf) -
每个项目是否总是以数字开头并以句点结尾?如果是这样,您可以使用更简单的模式:
\d[ .\dA-Za-z]+?\. -
不,客场以数字开头。但是当他一个人的时候。可以没有句号。
-
这是否意味着您的列表项不包含数字?您如何区分
4作为项目符号项和4作为文本中的数字? -
好点。我想到了这一点,只是假设只有数字作为项目符号。但由于所有都以句号结尾,我想我可以让它以单词和句号结尾。我试图让它以数字开头并以单词和句点结尾。不成功。我正在阅读更多内容并进行一些尝试