1

I have an issue with a regex expression and need some help. I have some expressions like these in mein .txt File:

19 = NAND (1, 19) 

regex expression : http://rubular.com/r/U8rO09bvTO

With this regex expression I got seperated matches for the numbers. But now I need a regex expression with an unknown amount of numbers in the bracket.

For example:

19 = NAND (1, 23, 13, 24)

match1: 19, match2: 1, match3: 23, match4: 13, match5: 24

I don't know the number of the numbers. So I need a main expression for min 2 numbers in the bracket till a unknow number.

By the way i'm using c++. @ Martjin Your first regex expression worked very well thanks. Here my code:

    boost::cmatch result;
    boost::regex matchNand ("([0-9]*) = NAND\\((.*?)\\)");
    boost::regex matchNumb ("(\\d+)");
    string cstring = "19 = NAND (1, 23, 13, 24)";
    boost::regex_search(cstring.c_str(), result, matchNand);
    cout << "NAND: " << result[1] << "=" << result[2] << endl;
    string str = result[2];
    boost::regex_search(str.c_str(), result, matchNumb);
    cout << "NUM: " << result[1] << "," << result[2]<< "," << result[3] << "," << result[4] << endl;

My output: NAND: 19=1, 23, 13, 24 NUM: 1,,,

So my new problem is i only find the first number. The result is also in complete opposite with this solution: http://rubular.com/r/nqXDSuBXjc

1 Answer 1

1

A simple (and maybe more clear than one regex) is to split this into two regexes.

First run a regex that splits your result from your arguments: http://rubular.com/r/YkGdkkg4y3

([0-9]*) = NAND \((.*?)\)

Then perform a regex that will match all the numbers in your argument: http://rubular.com/r/2vpSbZvz12

\d+

Assuming you're using Ruby, you can perform a regex that matches multiple times with the function scan as explained here: http://ruby-doc.org/core-1.9.3/String.html#method-i-scan

Of course you could just use the second regex with the scan function to get all the numbers from that line, but I'm guessing you're going to expand it even more, which is when this approach will be a little more structured.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the help now i have a new problem you can see it above
I'm not well versed in c++, but here's maybe some help: The function regex_search seems to only search for the regex once, and it will store every group (..) in the result array. Since our regex only has one group, it will only populate result[0]. How to get multiple matches was described here: stackoverflow.com/questions/3122344/…
I see now that the documentation also has an example on how to perform multiple matches, at the examples section: boost.org/doc/libs/1_52_0/libs/regex/doc/html/boost_regex/ref/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.