正则表达式:查找标签列表作为行尾

时间:2014-12-20 06:54:22

标签: c++ regex

我是正则表达式领域的新手。

我在这里要做的是验证用户在文本框中输入的数据。

我想要允许的内容: 正int值后跟mm/cm/km/in/ft

e.g. 1km, 12mm, 123in 

是允许的。

我在这里使用std::regex。 但是我在验证最后一个标签时遇到了困难,该标签可以是mm,cm,km,in,ft.

之一

我写过这样的话:

std::regex_match( str, std::regex("[0-9]*[^a-zA-Z]*in$") );
// But this will suffice only for the int values followd by inches,

我想知道如何写一个正则表达式,如果我们在值的末尾有mm,cm,km,in,ft之外的任何内容,它们将返回true。

2 个答案:

答案 0 :(得分:3)

std::regex_match( str, std::regex("[0-9]\*[^a-zA-Z]\*(mm|cm|km|in|ft)$") );

答案 1 :(得分:3)

您可以在正则表达式中使用替换|指定OR

[0-9]+(mm|cm|km|in|ft)$

Regex Demo

不久

[0-9]+([mck]m|in|ft)$

Regex Demo