How can I get the index of immediate before special characters of a word in c#
-
My question is: I want to know the index of /* and */ coming immediate before alpha2 Example: hello world /* this is */ right way to get the index of /* track to */ alpha2 dummy text alpha dummy /* test */textalpha dummy text Thanks in advance
-
My question is: I want to know the index of /* and */ coming immediate before alpha2 Example: hello world /* this is */ right way to get the index of /* track to */ alpha2 dummy text alpha dummy /* test */textalpha dummy text Thanks in advance
A Regular Expression[^] is probably the simplest way:
string input = @"hello world /* this is */ right way to get the index of /* track to */ alpha2 dummy text alpha dummy /* test */textalpha dummy text";
Regex pattern = new Regex(@"\/\*[^\*\/]*\*\/\s+alpha2");
Match match = pattern.Match(input);
if (match.Success)
{
int theIndex = match.Index;
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
A Regular Expression[^] is probably the simplest way:
string input = @"hello world /* this is */ right way to get the index of /* track to */ alpha2 dummy text alpha dummy /* test */textalpha dummy text";
Regex pattern = new Regex(@"\/\*[^\*\/]*\*\/\s+alpha2");
Match match = pattern.Match(input);
if (match.Success)
{
int theIndex = match.Index;
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks Homer, Its good but is there any way without the regular expression as I have to search from a file and there are a lot of same keywords like alpha2. with this regular expression It will be difficult to find specific instance and its immediate before characters. Actually I have to track the keywords which are not coming in comments section. Not coming in /* */ and not started with //. comment may be of many lines. Can you please suggest the solution. Thanks in advance. Rohit
-
Thanks Homer, Its good but is there any way without the regular expression as I have to search from a file and there are a lot of same keywords like alpha2. with this regular expression It will be difficult to find specific instance and its immediate before characters. Actually I have to track the keywords which are not coming in comments section. Not coming in /* */ and not started with //. comment may be of many lines. Can you please suggest the solution. Thanks in advance. Rohit