只返回正则表达式匹配的一部分 [英] Return only part of regex match

查看:110
本文介绍了只返回正则表达式匹配的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我认为x(?< return> String)x应该匹配xStringx并返回String,但它返回了整个事情。



我的模式是

((。* |(?<!。))(?< return> String)| (?< =。)<(?< return2> String)>)(。* |(?!。))





如果我在String goaway String上运行它,匹配将返回整个事物而不是两个String捕获。



我是什么尝试过:



--------------------------- ----------

解决方案

解决方案写在异常语法突出显示问题 [ ^ ]


如果我理解你,你想要完全匹配。所以,检查 IndexOf 方法:

String.IndexOf方法(字符串)(系统) [ ^ ]

如何:在字符串中搜索(Visual Basic)| Microsoft Docs [ ^ ]



欲了解更多详情,请参阅:

文化如何影响Visual Basic中的字符串| Microsoft Docs [ ^ ]

执行文化不敏感的字符串比较Microsoft Docs [ ^ ]

< PAVE OVER>比较和排序特定文化的数据 [ ^ ]







示例:

  Dim  sSearchedString  As  < span class =code-keyword> String  =   WhateverXstringYboolXstringZ是lorem ipsum string bool string 
Dim sKeys As String ()= 字符串(){ bool string}
Dim sb As StringBuilder = StringBuilder()
Dim 计数器作为 整数 = 0 ,i as 整数 = 0

对于 counter = sKeys.GetLowerBound( 0 sKeys.GetUpperBound( 0
i = sSearchedString.IndexOf(sKeys(counter), 0
执行 i> -1
sb.Append( String .Format( '{0}'已在位置找到:{1} {2} ,sKeys(counter),i,Microsoft.VisualBasic.vbCrLf))
i = sSearchedString.IndexOf(sKeys(counter),i + 1)
循环
下一步
Console.WriteLine( {0},sb.ToString())





退货:

<前lang =text>'bool'的地址:16
'bool'的地点:51
找到'string'的位置:9
'string'在位置找到:21
'string'在位置找到:44
'string'在位置被找到:56


几件事:

模式结尾处的最终捕获组是:

  (。* |(?!。))

这是一个非常奇怪的事情:

捕获

  一个空格后跟0或更多任何字符



  匹配后缀(任何单个字符)不存在。



整个模式的其余部分是单个captu re group所以无论它的各个部分匹配也在该组中作为一个整体捕获。



返回的匹配 Regex.Match()将始终报告导致匹配成功的输入的整个部分 Value 。这与 match.Groups(0)相同其他编号的捕获组开始.Groups(1)



您提供的模式和示例字符串返回:

匹配。 Groups.Count = 6 
match.Groups(0)=String goaway String
match.Groups(1)=String goaway String
match.Groups(2)=String goaway
match.Groups(3)=
match.Groups(4)=String
match.Groups(5)=
match.Groups( return)=字符串
match.Groups(return2)=

我建议使用 Expresso [ ^ ]这是一个很好的用于构建和测试正则表达式的免费工具。


Hi,

I thought x(?<return>String)x was supposed to match "xStringx" and return "String", but it's returning the entire thing.

My pattern is

((.* |(?<!.))(?<return>String)|(?<=.)<(?<return2>String)>)( .*|(?!.))



If I run this on "String goaway String", the match returns the entire thing rather than two captures of "String".

What I have tried:

-------------------------------------

解决方案

Solution written at Unusual syntax highlighting issue[^]


If i understand you well, you want to get exact match. So, check IndexOf method:
String.IndexOf Method (String) (System)[^]
How to: Search Within a String (Visual Basic) | Microsoft Docs[^]

For further details, please see:
How Culture Affects Strings in Visual Basic | Microsoft Docs[^]
Performing Culture-Insensitive String Comparisons | Microsoft Docs[^]
<PAVE OVER> Comparing and Sorting Data for a Specific Culture[^]

[EDIT]

Example:

Dim sSearchedString As String = "WhateverXstringYboolXstringZ is lorem ipsum string bool string"
Dim sKeys As String() = New String(){"bool", "string"}
Dim sb As StringBuilder = New StringBuilder()
Dim counter As Integer = 0, i As Integer = 0

For counter = sKeys.GetLowerBound(0) To sKeys.GetUpperBound(0)
	i = sSearchedString.IndexOf(sKeys(counter), 0)
	Do While i>-1
		sb.Append(String.Format("'{0}' has been found at position: {1}{2}" ,sKeys(counter), i, Microsoft.VisualBasic.vbCrLf)) 
		i = sSearchedString.IndexOf(sKeys(counter), i+1)
	Loop
Next
Console.WriteLine("{0}", sb.ToString())



Returns:

'bool' has been found at position: 16
'bool' has been found at position: 51
'string' has been found at position: 9
'string' has been found at position: 21
'string' has been found at position: 44
'string' has been found at position: 56


Several things:
The final capture group at the end of the pattern is:
 ( .*|(?!.))
which is a pretty strange thing:
capture either:
  a space followed by 0 or more any character
or
  Match if suffix (any single character) is absent.

The whole rest of the pattern is a single capture group so whatever its parts matches is also captured as a whole in that group.

The Match returned by Regex.Match() will always report as its Value the entire part of the input that caused the match to succeed. This is the same as match.Groups(0) The other numbered capture groups start at .Groups(1).

The pattern and example string you gave returns:

match.Groups.Count = 6
match.Groups(0) = "String goaway String"
match.Groups(1) = "String goaway String"
match.Groups(2) = "String goaway "
match.Groups(3) = ""
match.Groups(4) = "String"
match.Groups(5) = ""
match.Groups("return") = "String"
match.Groups("return2") = ""

I suggest using Expresso[^] which is an excellent FREE tool for building and testing regular expressions.


这篇关于只返回正则表达式匹配的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆