Solved

Is my Regex Syntax correct in C#

Posted on 2011-03-25
10
303 Views
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

0
Comment
Question by:tcalbaz
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
To look for a simple string, Regex is a bit overpowered - just IndexOf should do for you.
0
 
LVL 1

Author Comment

by:tcalbaz
Comment Utility
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
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 125 total points
Comment Utility
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
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
Comment Utility
>>  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
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
>>  " 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
 
LVL 4

Expert Comment

by:coolcurrent4u
Comment Utility
you can use


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

.....
end if

Open in new window

0
 
LVL 1

Author Closing Comment

by:tcalbaz
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Glad to be of service  = )
0
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
How civilized, it's been fun
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now