R
R
rusianvodka2014-05-03 17:02:50
C++ / C#
rusianvodka, 2014-05-03 17:02:50

Regular expressions, Search for all kinds of numbers in a string, CString

Hello Dear Ladies and Gentlemen.
Already what evening I suffer, I try to understand how to solve the task.
There is a line:

CString str = "	int z, a=245;int b = 4.6;int b = 0xa11;int c = 011; for(int a = 1;i<123; i++) ";a=437-(-0xab24)"

The task is to pull out all the numbers from it.
With the help:
str.FindOneOf(L"01234567890");
It turns out to find a number (of any kind, which is very important).
With this function, I convert a number from a string type to an integer.
swscanf_s(str, L"%i", &a);
But if you need to find a negative number, there are problems. The
str.FindOneOf(L"-01234567890");
first occurrence of a character without a digit is found.
Therefore, as I realized, regular expressions std regex will help me in solving the problem (if this is not the case, I will be happy with options for a different solution to this problem).
Unfortunately, I am a complete layman in this technology, so I would like to ask for help, advice on good resources, books, articles where you can get the necessary knowledge.
If you write a template for solving the task, I will also be very grateful, I will understand from it what is happening.
I will also ask a question in advance, if you solve the problem without signs, then everything is facilitated and regular expressions are not needed.
Since swscanf_s needs to pass a line where the first characters are the required translation number.
And FindOneOf returns only the number from the beginning of the string.
Then the problem arises, how to pass the address of the place, the found number in the string.
I solved this problem by clearing the line to the required number. (If there are other options that are simpler or more convenient, I will also be grateful.)
For example:
CString str = "	int z, a=245;int b = 4.6;int b = 0xa11;int c = 011; for(int a = 1;i<123; i++) ";a=437-(-0xab24)"

After my bydlocking, the line becomes:
CString str = "	         245;int b = 4.6;int b = 0xa11;int c = 011; for(int a = 1;i<123; i++) ";a=437-(-0xab24)"

Further passing str to swscanf_s the number is perfectly recognized.
But another problem arises.
How to force FindOneOf to go to the search for the next number 4.6.
There was an idea to kill the number 245 in a line, but I can’t even imagine how to implement it.
Perhaps regular expressions will also save me.
I hope there are regular expression gurus who will master this sheet.
And I will be very grateful to you for advice and help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bogolt, 2014-05-03
@bogolt

1. All adequate search functions take into account the possibility of starting the search from a certain position.
2. How will your algorithm extract the number 0xa11 ?
3. I don't understand why regular expressions are needed here. All IMHO can be completely done with a simple algorithm:
Break into lines Break
each line into separate words (divide by spaces, punctuation marks (excluding the dot which can be a separator of fractional numbers).
Check each word for the characters of which it consists - you have already implemented this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question