[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

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


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";
}

Open in new window

0
tcalbaz
Asked:
tcalbaz
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
Jens FiedererCommented:
To look for a simple string, Regex is a bit overpowered - just IndexOf should do for you.
0
 
tcalbazAuthor Commented:
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,
0
 
Jens FiedererCommented:
That is the correct syntax, assuming user_agent is the string within which you wish to search.

The starting position of the substring you found will be at q.Index (0-based, add one if you want your ordinals to start with 1).

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
>>  However, I did try IndexOf first and it seemed to me to be obtuse

Interesting. I've always found IndexOf() to be pretty straight-forward. The main benefit of regex over a simple IndexOf() is that you can make the search case-insenstive. In this way, you could pass "findme" as the string to find and locate any of the following:

findme
FINDME
FiNdMe

You can also use the Contains() method (though it suffers from the same negative as IndexOf()):
Dim source As String = "Luke, come to the dark side!"
Dim found As Boolean = source.Contains("dark")

Open in new window

0
 
Jens FiedererCommented:
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).
0
 
käµfm³d 👽Commented:
>>  " 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.
0
 
coolcurrent4uCommented:
you can use


If Regex.IsMatch(strlText, "^error") Then

.....
end if

Open in new window

0
 
tcalbazAuthor Commented:
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
0
 
käµfm³d 👽Commented:
Glad to be of service  = )
0
 
Jens FiedererCommented:
How civilized, it's been fun
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now