Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I have below text from which I want to find only "Two" text

Two: 2
Twenty Two: 22
Fifty Two: 52


If I fire below regular-expression, I getting 3 matches as I want to find only "Two"

-----------------------
Two: (?'two')
-----------------------



Kindly update.

Rishi
Posted
Comments
Maciej Los 24-Sep-13 8:10am    
'Twenty Two' contains a 'Two' word...
yrishi 24-Sep-13 8:15am    
sorry??
Maciej Los 24-Sep-13 8:20am    
I'm trying to tell you that regex return proper matches ;)

Try: ^Two as pattern,
^ means: match start of string ;)
 
Share this answer
 
I'm not sure exactly what you are looking for, but to ensure that the line contains "two" only, without other words, match to the beginning of the line:
^Two: (?'two'\d+)$


The named group "(?'two'\d+)" will capture all digits to the end of the line and put them in a group called "two".
 
Share this answer
 
v2
Comments
yrishi 24-Sep-13 8:28am    
Thanks for your comment. I want to capture only "Two: 2". In this case match count should be 1..
Brian A Stephens 24-Sep-13 9:00am    
What result are you getting? The caret ^ ensures that the line has to start with the matching text, so it should match only one line from your sample.
yrishi 24-Sep-13 9:27am    
getting zero count.
Brian A Stephens 24-Sep-13 10:52am    
Be sure you specify the RegexOptions.Multiline option to change the meaning of ^ and $ to be per line.
yrishi 25-Sep-13 4:22am    
still getting zero count for below code
MatchCollection matches = Regex.Matches(Txt, regX, RegexOptions.Multiline);
Tried with below regular-expression.. no success

^Two: (?'two'\d+)$

Please revert
 
Share this answer
 
Comments
Maciej Los 24-Sep-13 9:13am    
This is not an answer. Please delete it to avoid down-voting.
yrishi 24-Sep-13 9:16am    
:(
Its done by using below regular-expression

^Two:

:)
 
Share this answer
 

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