Click here to Skip to main content
14,732,788 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi, I'm using VS2008 MFC VC++ and trying to get dynamic numbers from a string and paste them into another string.

Let's say there is a dialog and get the window caption as String A:

String A (input string):
Voltage: 2.0V, Current:0.4A, Resistance: 5.0Ω, Power: 1.5W.

String A is also a dynamic input. It could be
Voltage: 30V, Current:1.2A, Resistance: 25Ω, Power: 36W.

or next time could be
The apple is inside column: 12, row: 3, box: 5.

For String B, it is exactly the same as String A except the numbers are replaced by delimiter. It is used as the reference to extract the numbers from String A.

String B (input reference string):
Voltage: %fV, Current:%fA, Resistance: %fΩ, Power: %fW.

Then this is String C,

String C (output reference string):
The answers are %fV; %fA; %fΩ; %fW.

Therefore, how can I extract the 4 sets of number and paste them into String C and get the final output as
The answers are 2.0V; 0.4A; 5.0Ω; 1.5W.

From my understanding on regex, is the String B (input reference string) will be the pattern that used to search/match the String A? Any idea?


What I have tried:

I tried to implement the split and merge method but it seems not possible for this situation because there are too much possibility from the input string.
Updated 25-Oct-20 23:25pm

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre -[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
Numbers aren't difficult to extract with a regex:
will do it, it matches one or more digits optionally followed by a decimal point and one or more digits.
Josh_0101 29-Oct-20 4:42am
There is an issue, what if there are different data types of number included in the same string? int; float; double;
OriginalGriff 29-Oct-20 6:12am
What do you think? The regex will still pick it up ... the processing of the numbers string after that is down to your app!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900