Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Prostate Specific Antigen 1.2 NA NA . . . . . . . Age yrs up to 49 50 - 59 60 - 69 over

I want to match "Prostate Specific Antigen" this word and need to get following word


Expected Result:
1.2

Possibilities
<1.2 or >1.2 or <=1.2, >=1.2

Any special characters or any no of special characters will come before 1.2 but I need to select that word

What I have tried:

string Pattern = Prostate Specific Antigen\s*(\d+(\.\d*)?|\.\d+);
Match matches = Regex.Match(RawContent, Pattern, RegexOptions.IgnoreCase);

if (matches.Success)
{
MessageBox.Show(matches.Groups[1].Value.ToString());
MessageBox.Show(matches.Value.ToString());
}
Posted
Updated 8-Nov-16 1:40am

I prefer a non-regex approach:

C#
string text = "Prostate Specific Antigen 1.2 NA NA . . . . . . . Age yrs up to 49 50 - 59 60 - 69 over";
string searchTerm = "Prostate Specific Antigen";
string value = string.Empty;
int pos = text.IndexOf(searchTerm);
if (pos >= 0)
{
    string temp = text.Substring(pos + searchTerm.Length).Trim();
    string[] parts = temp.Split(' ');
    value = parts[0];
}


I tested the code with the string shown, with text prepended to the string, with nothing but the search term in the string, and finally, an empty string.
 
Share this answer
 
v2
Comments
Kornfeld Eliyahu Peter 8-Nov-16 9:27am    
It may be the best solution, as it easily can be ported to other languages with different regex support from .NET...
Subashchandrabose31 9-Oct-18 0:16am    
Thank you so much ....
Member 12491629 7-Apr-20 17:19pm    
thank you very much
You can use 'look behind' grouping for that, but it is not supported everywhere (in .NET you have it)
Regular Expression Language - Quick Reference[^]

(?<=\bProstate Specific Antigen\s)(\w+)
 
Share this answer
 
Try RegEx: "Prostate Specific Antigen\s+([^ ]+)\s"
C#
string Pattern = "Prostate\\ Specific\\ Antigen\\s{1,}([^\\ ]+)\\s";


Here is a 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
Comments
Kornfeld Eliyahu Peter 8-Nov-16 9:28am    
It will match the whole text including the prefix 'Prostate Specific Antigen', and not only the 'word' after it...

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