gpt4 book ai didi

python RE : check if word is only before or after another word

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:18 24 4
gpt4 key购买 nike

我正在尝试将变量映射到 Excel 中的特定列。假设我想捕获:

2.4 string
string 2.4

但不是:

2.4 string 2.4
string

我使用了这个表达式,但是那个表达式匹配了我不想要的 "2.4 string 2.4":

 r"([2]\.[4])?\s*(\"|'|inch)?\s*String\s*([2]\.[4])?\s*(\"|'|inch)?"

当用于字符串 "2.4 string" 时,group() 函数仅返回 "string" 而不是 "2.4 字符串”

有人可能知道解决方案吗?

最佳答案

你可以使用这个正则表达式:

^(?:2\.4 string|string 2\.4)$

示例 VBScript:

Dim myRegExp
Set myRegExp = New RegExp
myRegExp.Pattern = "^(?:2\.4 string|string 2\.4)$"
If myRegExp.Test(SubjectString) Then
' Successful match
Else
' Match attempt failed
End If

解释

  • ^ anchor 断言我们在字符串的开头
  • `(?: ... ) 是匹配...的非捕获组
  • 2.4 字符串
  • |
  • 字符串 2.4
  • $ anchor 断言我们在字符串的末尾

关于 python RE : check if word is only before or after another word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24881868/

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