Solved

Regex help please

Posted on 2006-10-23
5
258 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 Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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