简体   繁体   中英

C# Regex Retrieve multiple values based on a single pattern

I have the following string:

"483 432,96 (HM: 369 694,86; ZP: 32 143,48; NP: 4 507,19; SP: 40 800,62; SDS: 4 389,84; IP: 9 497,14; PvN: 3 157,25; ÚP: 3 102,14; GP: 808,28; PRFS: 15 332,16)"

What I am trying to do, is to retrieve all values (if they exist) for the following letters (I highlighted necessary values in bold below):

483 432,96 (HM: 369 694,86; ZP: 32 143,48; NP: 4 507,19; SP: 40 800,62; SDS: 4 389,84; IP: 9 497,14; PvN: 3 157,25; ÚP: 3 102,14; GP: 808,28; PRFS: 15 332,16 )

I tried to retrieve values one by one with the following regex:

string regex = "NP: ^[0-9]^[\\s\\d]([.,\\s\\d][0-9]{1,4})?$";

But with no luck either (I am a newbie in Regex patterns).

Is it possible to retrieve all values in a one string (and then simply loop through the results), or do I have to go one key at the time?

Here is my full code:

string sTest = "483 432,96 (HM: 369 694,86; ZP: 32 143,48; NP: 4 507,19; SP: 40 800,62; SDS: 4 389,84; IP: 9 497,14; PvN: 3 157,25; ÚP: 3 102,14; GP: 808,28; PRFS: 15 332,16)";
string regex = "NP: ^[0-9]^[\\s\\d]([.,\\s\\d][0-9]{1,4})?$";
System.Text.RegularExpressions.MatchCollection coll = System.Text.RegularExpressions.Regex.Matches(sTest, regex);
String result = coll[0].Groups[1].Value;

You can't get them all with one regex, unless you are absolutely sure that they will all appear next to each other. Also, what would be the point of getting them all and having to split the result afterwards anyway. Here is a regex which would find the values you wanted:

(ZP|NP|SP|SDS|IP|PvN|ÚP|GP|PRFS): ([^;)]+)

Now the first group will be the key and the second group will be the value.

The idea is:

  • (x|y|z) matches either x or y or z
  • [^;)]+ matches something, which is not ; (because this is how they are currently delimited) or ) (for the last position) one or more times

I tried to retrieve values one by one with the following regex:

Let's fix your one-by-one regex:

  • Caret ^ outside the [] character class means "start of line", so your expression with two carets in different places will not match anything.
  • Use \\d instead of [0-9] and \\D instead of [^0-9]

Here is one expression that matches NP: pattern ( demo ):

NP: \d+\D\d+([.,]\d{1,4})?

Now convert it to an expression that matches other tags like this:

(NP|ZP|SP|...): \d+\D\d+([.,]\d{1,4})?

Applying this pattern in a loop repeatedly will let you extract the tags one by one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM