Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Is my Regex Syntax correct in C#

Posted on 2011-03-25
Medium Priority
Last Modified: 2012-05-11
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

Question by:tcalbaz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
LVL 23

Expert Comment

by:Jens Fiederer
ID: 35218280
To look for a simple string, Regex is a bit overpowered - just IndexOf should do for you.

Author Comment

ID: 35218852

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.

LVL 23

Accepted Solution

Jens Fiederer earned 500 total points
ID: 35218918
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).


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 35219740
>>  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:


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

LVL 23

Expert Comment

by:Jens Fiederer
ID: 35219775
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).
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35220053
>>  " 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.

Expert Comment

ID: 35221490
you can use

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

end if

Open in new window


Author Closing Comment

ID: 35224042
This was an interesting discussion and I found  both your answers very helpful.  

I never knew about the 'contains' keyword.  Thank you so much!

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35228464
Glad to be of service  = )
LVL 23

Expert Comment

by:Jens Fiederer
ID: 35228780
How civilized, it's been fun

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question