Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regex string parsing

Tags:

c#

.net

regex

I have the expression already written, but whenever I run the code I get the entire string and a whole bunch of null values:

Regex regex = new Regex(@"y=\([0-9]\)\([0-9]\)(\s|)\+(\s+|)[0-9]");
Match match = regex.Match("y=(4)(5)+6");

for (int i = 0; i < match.Length; i++)
{
    MessageBox.Show(i+"---"+match.Groups[i].Value);
}

Expected output: 4, 5, 6 (in different MessageBoxes

Actual output: y=(4)(5)+6

It finds if the entered string is correct, but once it does I can't get the specific values (the 4, 5, and 6). What can I do to possibly get that code? This is probably something very simple, but I've tried looking at the MSDN match.NextMatch article and that doesn't seem to help either.

Thank you!

like image 801
Scott Avatar asked Oct 04 '12 12:10

Scott


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

As it currently is, you don't have any groups specified. (Except for around the spaces.)

You can specify groups using parenthesis. The parenthesis you are currently using have backslashes, so they are being used as part of the matching. Add an extra set of parenthesis inside of those.

Like so:

new Regex(@"y=\(([0-9]+)\)\(([0-9]+)\)\+([0-9]+)");

And with spaces:

new Regex(@"y\s*=\s*\(([0-9]+)\)\s*\(([0-9]+)\)\s*\+\s*([0-9]+)");

This will also allow for spaces between the parts to be optional, since * means 0 or more. This is better than (?:\s+|) that was given above, since you don't need a group for the spaces. It is also better since the pipe means 'or'. What you are saying with \s+| is "One or more spaces OR nothing". This is the same as \s*, which would be "Zero or more spaces".

Also, I used [0-9]+, because that means 1 or more digits. This allows numbers with multiple digits, like 10 or 100, to be matched. And another side note, using [0-9] is better than \d since \d refers to more than just the numbers we are used to.

like image 80
LTAcosta Avatar answered Oct 05 '22 22:10

LTAcosta