Solved

Regex help please

Posted on 2006-10-23
5
252 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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