Solved

Regex help please

Posted on 2006-10-23
5
266 Views
Last Modified: 2010-04-16
      I'm trying to check if a string contains a letter or a strange character and if it does change the value of success to true, otherwise set the value of success to false. What I have done so far isn't working - can somebody tell me what I'm doing wrong?                  

                        string success;
                        if(Regex.Match(line.Substring(304,8).ToUpper(),@"^(0*)(?<number>\d+)[^\d]+$"))
                        {
                            success = "true";
                        }
                        else
                        {
                            success = "false";
                        }
0
Comment
Question by:boukaka
[X]
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
  • 3
5 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17789559
Hi boukaka;

What characters do you consider "strange character"?

What set of characters do you want in index position 304 for a length of 8 in the string line?

Fernando
0
 

Author Comment

by:boukaka
ID: 17789582
line.Substring(304,8) could show up as any one of the following examples and more 00000430 or 0000043} or 0003456M etc etc.

What I'm trying to do is read that substring, if it contains a curly bracket or a letter, change success to "true" otherwise change success to "false".

It's not the Regex check itself that's the problem, it's the way I'm wording my IF statement. I get the error that says that I cannot implicitly assign the value of bool to Regex.
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 350 total points
ID: 17789616
Hi, you need to use the IsMatch method as it returns a boolean:

                        string success;

                        if(Regex.IsMatch(line.Substring(304,8).ToUpper(),@"^(0*)(?<number>\d+)[^\d]+$"))
                        {
                            success = "true";
                        }
                        else
                        {
                            success = "false";
                        }
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17789666
Hi Hi boukaka;

Replace your if statement with the following code.

      string success;
      success = Regex.IsMatch(line.Substring(304,8).ToUpper(),@"^\d{7}[}A-Z]$");

Fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17789742
Sorry that should be

     string success;
     success = Regex.IsMatch(line.Substring(304,8).ToUpper(),@"^\d{7}[}A-Z]$").ToString().ToLower();

That is because success is a string and not a bool.

Fernando
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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