Solved

Regex help please

Posted on 2006-10-23
5
227 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
  • 3
5 Comments
 
LVL 62

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 62

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 62

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

16 Experts available now in Live!

Get 1:1 Help Now