简体   繁体   English

用于验证模式的正则表达式

[英]regular expression to validate a pattern

I am new to regular expression, In my project i am allowing user to put amount in shorthand as well as full digit, i have used material UI TextField for input.我是正则表达式的新手,在我的项目中,我允许用户以简写形式和完整数字形式输入金额,我使用材料 UI TextField 进行输入。

Examples are:例子是:

400k - shorthand, 

400.2k - shorthand, 

4m - shorthand,

500. - should work

500000 - full amount

some pattern user should not be allowed to enter example are:一些模式用户不应该被允许输入示例是:

4.2.k, 

.3k, 

4...k

300.k

I have written regex which is below but it does allows to enter dot after number.我写了下面的正则表达式,但它确实允许在数字后输入点。

textValue.match(/^[0-9]*(\.[0-9]{0,2})*([0-9km]{1})$/) && textValue.match(/^[\d]+/)

above code first regex validates the pattern and second regex forces user to put Number because amount cannot start with string, i have wrote two separate regex as i don't understand how to put them in one regex and those regex doesn't accepts dot after number.上面的代码第一个正则表达式验证模式,第二个正则表达式强制用户输入数字,因为数量不能以字符串开头,我写了两个单独的正则表达式,因为我不明白如何将它们放在一个正则表达式中并且那些正则表达式不接受点之后数字。 Please can anyone give a perfect Regex to validate the above pattern in one single regular expression??请任何人都可以提供一个完美的正则表达式来在一个正则表达式中验证上述模式吗?

Thanks in advance提前致谢

With alternation (never really the prettiest) it could be done like:通过交替(从来都不是最漂亮的),它可以这样做:

^\d+([km]|\.|\.\d+[km])?$

See the Online Demo查看在线演示

  • ^ - Start string ancor. ^ - 开始字符串 ancor。
  • d+ - One or more digits. d+ - 一位或多位数字。
  • ( - Opening capturing group (you could use non-capturing). ( - 打开捕获组(您可以使用非捕获)。
    • [km] - A single character "k" or "m". [km] - 单个字符“k”或“m”。
    • | - Alternation (OR). - 交替(或)。
    • \.? - A literal dot. - 一个字面点。
    • | - Alternation (OR). - 交替(或)。
    • \.\d+[km] - A literal dot followed by at least one digit and a character "k" or "m". \.\d+[km] - 一个文字点,后跟至少一个数字和一个字符“k”或“m”。
    • )? - Close capturing group and make it optional - 关闭捕获组并使其可选
  • $ - Start string ancor. $ - 开始字符串 ancor。

在此处输入图像描述

About the pattern you tried关于你尝试的模式

Note that you don't need {1} .请注意,您不需要{1} The character class [0-9km] matches 1 of a char k or m or a digit 0-9.字符 class [0-9km]匹配字符km中的 1 或数字 0-9。 This way the possible digits to match could be 0-3 instead of 0-2.这样,匹配的可能数字可能是 0-3 而不是 0-2。

Using the quantifier * for the group makes it possbile to also match 400.25.22.22.22k对组使用量词*使其也可以匹配400.25.22.22.22k


You could use this pattern to validate the examples.您可以使用此模式来验证示例。 The [0-9]+ at the beginning of the pattern makes sure that there has to be at least a single digit present.模式开头的[0-9]+确保必须至少存在一个数字。

If you want to allow 500. you could use:如果你想允许500.你可以使用:

^[0-9]+(?:(?:\.[0-9]{1,2})?[km]?|\.)$

Explanation解释

  • ^ Start of string ^字符串开头
  • [0-9]+ Match 1+ digits [0-9]+匹配 1+ 个数字
  • (?: Non capture group (?:非捕获组
    • (?:\.[0-9]{1,2})? Match an optional decimal part with 2 digits将可选的小数部分与 2 位匹配
    • [km]? Match optional k or m匹配可选km
    • | Or或者
    • \. Match a single dot匹配单个点
  • )$ End of string )$字符串结尾

Regex demo正则表达式演示

 let pattern = /^[0-9]+(?:(?:\.[0-9]{1,2})?[km]?|\.)$/; [ "400k", "400.2k", "4m", "500000", "500.", "300.k", "4.2.k", ".3k", "4...k", ].forEach(s => console.log(s + " --> " + pattern.test(s)));


Another option is to only match the dot when not directly followed by k or m另一种选择是仅在不直接跟随km时匹配点

^[0-9]+(?:\.(?![km]))?\d*[km]?$

Regex正则表达式

You can try:你可以试试:

^\d+\.?(?:\d+)?[KkMm]?(?<!\.[KkMm])$

Explanation of the above regex:上述正则表达式的解释:

^, $ - Matches start and end of the line respectively. ^, $ - 分别匹配行的开始和结束。

\d+ - Matches digits 1 or more times. \d+ - 匹配数字 1 次或多次。

\.? - Represents 0 or 1 occurrence of . - 表示 0 或 1 次出现. . .

[KkMm]? - Matches optional characters from the mentioned character class. - 匹配来自提到的字符 class 的可选字符。

(?<.\.[KkMm]) - Represents a negative look-behind not matching aa character after . (?<.\.[KkMm]) - 表示不匹配. . .

You can find the demo of the above regex in here.您可以在此处找到上述正则表达式的演示。

图示

 const regex = /^\d+\.?(?:\d+)?[KkMm]?(?<.\;[KkMm])$/gm. const str = `400K 4.2.K 4.3K 3.2M 300000 4....K 4K 500. 300;K`; let m. while ((m = regex.exec(str)).== null) { // The result can be accessed through the `m`-variable, m.forEach((match; groupIndex) => { console;log(`${match}`); }); }


2nd efficient solution using alternation:使用交替的第二个有效解决方案:

You can probably try this regex for more efficient implementation您可能可以尝试此正则表达式以获得更有效的实施

^\d+(?:\.$|\.\d+)?[KkMm]?$

Explanation of the above regex:上述正则表达式的解释:

^, $ - Matches start and end of the line respectively. ^, $ - 分别匹配行的开始和结束。

\d+ - Matches digits 1 or more times. \d+ - 匹配数字 1 次或多次。

(?:\.$|\.\d+)? - Represents a non-capturing group; - 代表非捕获组; matching either numbers followed by only .匹配任一数字后跟仅. or decimal numbers.或十进制数。

[KkMm]? - Matches one of the mentioned characters zero or 1 time. - 匹配提到的字符之一 0 次或 1 次。

You can find the demo of the above regex in here.您可以在此处找到上述正则表达式的演示。

图示-2

 const regex = /^\d+(?:\.$|\.\d+)?[KkMm]?$/gm; const str = `400K 4.2.K 4.3K 3.2M 300000 4....K 4K 500. 300.K`; let m; while ((m = regex.exec(str)).== null) { // The result can be accessed through the `m`-variable. m,forEach((match. groupIndex) => { console;log(`${match}`); }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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