简体   繁体   中英

How to Optimize these regex?

I have a pattern that match many simple cases. I can use regex for each of them but it cost a lot of performance. How can I optimize them ? The pattern is mainly from a width x height values from 1~99 in 1 decimal with or without "m" (meter)

Pattern (each values can have "m" after or not)

23x10
23 x 10
23 x10
23x 10
2.3x10
2.3 x 10
2.3 x10
2.3x 10
2.3x5.1
2.3 x 5.1
2.3x 5.1
2.3 x5.1
1x5
2 x 5
3 x4
4x 5
...etc

What I done up to now:

I tried each regex for each pattern but it may take a lots of pattern which reduce performance

static const QString c_Pattern_WidthHeight_1 = "\\dx\\d\\dm";                   //ex: 5x10m
static const QString c_Pattern_WidthHeight_2 = "\\d[,.]\\d x \\d\\dm";          //ex: 4.1 x 22m
static const QString c_Pattern_WidthHeight_3 = "\\d[,.]\\dx\\d\\dm";            //ex: 4.5x21m
static const QString c_Pattern_WidthHeight_4 = "\\dx \\d\\dm";                  //ex: 5x 14m
static const QString c_Pattern_WidthHeight_5 = "\\dx\\dm";                      //ex: 5x8m
static const QString c_Pattern_WidthHeight_6 = "\\d[,.]\\dm x \\d\\dm";         //ex: 3.8m x 10m
static const QString c_Pattern_WidthHeight_7 = "\\d[,.]\\d\\d x \\d";           //ex: 4.15 x 9
static const QString c_Pattern_WidthHeight_8 = "\\d x \\d\\dm";                 //ex: 3 x 10m
static const QString c_Pattern_WidthHeight_9 = "\\d\\dx\\d[,.]\\dm";            //ex: 10x4.5m

Try use this regex:

^\d{1,2}(\.\d{1,2})? ?m? ?x ?\d{1,2}(\.\d{1,2})? ?m?$

Here is Demo

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