Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

-------------------------------------
Posted
Updated 13-Jun-17 21:03pm
Comments
Maciej Los 13-Jun-17 15:28pm    
So... What is the question?
[no name] 13-Jun-17 16:33pm    
Maciej I knew you'd ask that. I thought it would be obvious. As I'm surprised that I'm getting the entire string rather than just "String" and I've posted a question about it, common sense would tell you I need help to figure out why that's happening and how to resolve it.
Maciej Los 13-Jun-17 17:03pm    
OK. Seems you want to return exact match. Can you provide sample input and expected output? Use "Improve question" widget (right-bottom corner of your question).
Richard MacCutchan 13-Jun-17 15:56pm    
Maybe regex is not the optimum tool for what you are trying to do.
PIEBALDconsult 13-Jun-17 19:27pm    
If only there were something like a... https://www.codeproject.com/Articles/39165/RegexTester

Solution written at Unusual syntax highlighting issue[^]
 
Share this answer
 
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:
VB.NET
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
 
Share this answer
 
v3
Comments
[no name] 13-Jun-17 18:05pm    
String.IndexOf won't work because there can be something like

"int String String"

So the 2nd String will be ignored, and IndexOf("boolean") will result in -1.
Maciej Los 14-Jun-17 2:40am    
It's not true. IndexOf method has many implementations and can be used within loop. You can use IndexOf(string, int32) method, where the second parameter is used to define start position to search within string. Check updated solution.
[no name] 14-Jun-17 3:03am    
I've managed to solve all of the issues at once and have posted the solution on my other question, https://www.codeproject.com/Questions/1191744/Unusual-syntax-highlighting-issue
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.
 
Share this answer
 
((.* |(?<!.))(?<return>String)|(?<=.)<(?<return2>String)>)( .*|(?!.))

I am curious to know in plain text what you try to match.
Use last link to see a nice diagram of your RegEx. It may help to understand what is going on.

Quote:
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900