简体   繁体   中英

ignore special characters at the end in regex

I want to split a comma separated list into groups, with this regex:
/(?<k>[\\d.]+)\\s(?<i>[\\w\\d\\p{L}\\s\\/.-]+(?:\\(.*?\\))?)/gu

For example it splits up "34 bananas, 12 l. applejuice." into

[0][k] = 34; [i] = bananas
[1][k] = 12; [i] = l. applejuice.

At the end of the text is a dot . which will be recognized as a part of the text. Since some parts can have a dot inside of it, like l. (the shortcut of liters does) i needed to implement it, too. But i don't need it at the end.

For a better explanation please see this regex101 .

In short: i need to ignore every comma , and dot . at the end of every value, but need to have inside of the strings. I tried it like [^,.] at the end, but it doesn't work. (?!,|\\.) at the end or the beginning doesn't work aswell.

You might use a negative lookbehind asserting what is directly to the left is not a dot.

\\w will also match \\d but as you use \\p{L} to match any kind of letter, you might omit \\w and add an underscore (which is matched by \\w )

/(?<k>[\d.]+)\s(?<i>[\p{L}\d_\s\/.-]+(?<!\.)(?:\([^()]*\))?)/gu

Regex demo

 let s = `34 bananas, 12 l. applejuice., Zusätzlich benötigte Fahrzeuge: 43 FuStW, 93 Löschfahrzeuge (LF), 86.375 l. Wasser, 300 l. Löschschaum, 25 Drehleitern (DLK 23), 2 FwK, 2 GW-A oder AB-Atemschutz, 10 Gerätekraftwagen (GKW), 10 THW-Einsatzleitung (MTW TZ), 1 Schlauchwagen (GW-L2 Wasser, SW 1000, SW 2000 oder Ähnliches), 10 ELW 1, 2 Dekon-P, 25 ELW 2, 1 GW-Messtechnik, 1 GW-Gefahrgut, 1 GW-Höhenrettung, 1 Rettungshundestaffel/n. Wir benötigen mindestens 12 Feuerwehrleute.`; let pattern = /(?<k>[\\d.]+)\\s(?<i>[\\p{L}\\d_\\s\\/.-]+(?<!\\.)(?:\\([^()]*\\))?)/gu console.log(s.match(pattern));

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