Solved

Regex help please

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sum Column in GridView 3 42
Two different visual studio versions 3 22
Connection String 16 43
C# MVC and a global static factory that never dies 4 46
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

912 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

18 Experts available now in Live!

Get 1:1 Help Now