tcalbaz
asked on
Is my Regex Syntax correct in C#
Most irrrefutable guru's,
As a C# newbie I am needing a reality check for correct syntax of a Regex statement where I am checking for the existance of a string within a string. I am basically looking for a replacement for the Mid$ function in VB.Net
Please correct and enlighten me if I am wrong.
Thank You
As a C# newbie I am needing a reality check for correct syntax of a Regex statement where I am checking for the existance of a string within a string. I am basically looking for a replacement for the Mid$ function in VB.Net
Please correct and enlighten me if I am wrong.
Thank You
using System;
using System.Text.RegularExpressions;
public string getBrowser()
{
string functionReturnValue = null
string searchforthis="Netscape";
Match q = Regex.Match(user_agent,searchforthis);
if (q.Success) {
functionReturnValue = "ns4";
}
To look for a simple string, Regex is a bit overpowered - just IndexOf should do for you.
ASKER
jensfiederer,
At heart, I would have to agree with you.
However, I did try IndexOf first and it seemed to me to be obtuse about searching for strings with more then one char character and in the end it got rather complex.
I found the Regex method a little more concise.
My question: is this correct syntax?
The compiler passed it but I don't know if is the correct approach. I want it give me the starting integer ordinal position of the substring within the string.
regards,
At heart, I would have to agree with you.
However, I did try IndexOf first and it seemed to me to be obtuse about searching for strings with more then one char character and in the end it got rather complex.
I found the Regex method a little more concise.
My question: is this correct syntax?
The compiler passed it but I don't know if is the correct approach. I want it give me the starting integer ordinal position of the substring within the string.
regards,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Contains would give him the same result as q.success, but not " the starting integer ordinal position of the substring within the string".
And, yes, any problems in IndexOf (which gives you just that, or -1 if not found) are likely to some confusion that resulted during use rather than any deficiencies in the method itself.
As kaufmed points out, the main reason for using regex is case-insensitivity (or more complex patterns, like strings of digits of unknown length).
And, yes, any problems in IndexOf (which gives you just that, or -1 if not found) are likely to some confusion that resulted during use rather than any deficiencies in the method itself.
As kaufmed points out, the main reason for using regex is case-insensitivity (or more complex patterns, like strings of digits of unknown length).
>> " the starting integer ordinal position of the substring within the string".
I missed that in the author's last post, but I agree, it would not give starting index--it would give "true" or "false" as demonstrated by the type of the variable storing the result. There is a discrepancy about what the desired result is--the OP demonstrates one type of return value; the last post indicates something different.
I missed that in the author's last post, but I agree, it would not give starting index--it would give "true" or "false" as demonstrated by the type of the variable storing the result. There is a discrepancy about what the desired result is--the OP demonstrates one type of return value; the last post indicates something different.
you can use
If Regex.IsMatch(strlText, "^error") Then
.....
end if
ASKER
Gentleman,
This was an interesting discussion and I found both your answers very helpful.
I never knew about the 'contains' keyword. Thank you so much!
Ted
This was an interesting discussion and I found both your answers very helpful.
I never knew about the 'contains' keyword. Thank you so much!
Ted
Glad to be of service = )
How civilized, it's been fun