gpt4 book ai didi

regex - 使用正则表达式将编号列表数组拆分为多行编号列表

转载 作者:行者123 更新时间:2023-12-03 03:29:44 25 4
gpt4 key购买 nike

我正在尝试学习正则表达式来回答有关葡萄牙语的问题。

输入(单元格上的数组或字符串,因此.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'

正在阅读thisthisRegExr Website与表达式 /([0-9]{1,2})([.]{0,1})([0-9]{0,2})/igm 一起使用输入。

得到以下结果:

RegExr

有更好的方法吗?正则表达式是正确的还是更好的生成方式?我在 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

enter image description here

注意

您可能要求匹配仅停在单词 + . 处,后跟 0+ 个空格和一个使用 \d+(?:\.\d+)*[\s\S]*?[a-zA-Z]+\.(?=\s*(?:\d+|$)) 的数字。 .

(?=\s*(?:\d+|$)) 正向先行要求存在 0+ 个空格 (\s*),后跟 1 + 数字 (\d+) 或字符串结尾 ($) 紧邻当前位置的右侧。

关于regex - 使用正则表达式将编号列表数组拆分为多行编号列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46331543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com