尝试匹配仅包含空格的字符串和正则表达式

时间:2022-01-21 16:51:33

I have a string that is converted into ascii code.

我有一个转换成ascii代码的字符串。

Exemple: " " (Three spaces) -> "323232"

例如:“”(三个空格) - >“323232”

I want for the regex to match the string ENTIRELY only if it has just spaces. I've tried endlessly with no luck. if there is another char other than space the regex wouldn't match.

我希望正则表达式匹配字符串ENTIRELY只有它只有空格。我无休止地试着没有运气。如果除了空格之外还有另一个字符,则正则表达式将不匹配。

Exemple: "3264" that will not match because it contains an 64 (A) in it.

例如:“3264”不匹配,因为它包含64(A)。

2 个解决方案

#1


1  

You can match something repeatedly with *, and you can apply that to a sequence of characters by enclosing them in parentheses. So, you want something like (32)*.

您可以使用*重复匹配某些内容,并且可以通过将它们括在括号中将其应用于字符序列。所以,你想要像(32)*这样的东西。

If you're using a "search" rather than a "match" (depends on your language, how you're using the regex, etc) then you probably need to anchor the regex to the start and end of your string. Something like: ^(32)*$. (^ usually means "start of string" and $ usually means "end of string".)

如果你使用的是“搜索”而不是“匹配”(取决于你的语言,你如何使用正则表达式等),那么你可能需要将正则表达式锚定到字符串的开头和结尾。类似的东西:^(32)* $。 (^通常表示“字符串的开头”,$通常表示“字符串的结尾”。)

#2


0  

You can then do something like this. The below is psedo-code

然后你可以做这样的事情。以下是psedo-code

if(str.replace(/32/g,"") == ""){
   // match
}

What it does, is basically remove all 32's and check if the resultant string is an empty one, which suggests it was full of spaces in ascii code.

它的作用基本上是删除所有的32并检查结果字符串是否为空,这表明它在ascii代码中充满了空格。

#1


1  

You can match something repeatedly with *, and you can apply that to a sequence of characters by enclosing them in parentheses. So, you want something like (32)*.

您可以使用*重复匹配某些内容,并且可以通过将它们括在括号中将其应用于字符序列。所以,你想要像(32)*这样的东西。

If you're using a "search" rather than a "match" (depends on your language, how you're using the regex, etc) then you probably need to anchor the regex to the start and end of your string. Something like: ^(32)*$. (^ usually means "start of string" and $ usually means "end of string".)

如果你使用的是“搜索”而不是“匹配”(取决于你的语言,你如何使用正则表达式等),那么你可能需要将正则表达式锚定到字符串的开头和结尾。类似的东西:^(32)* $。 (^通常表示“字符串的开头”,$通常表示“字符串的结尾”。)

#2


0  

You can then do something like this. The below is psedo-code

然后你可以做这样的事情。以下是psedo-code

if(str.replace(/32/g,"") == ""){
   // match
}

What it does, is basically remove all 32's and check if the resultant string is an empty one, which suggests it was full of spaces in ascii code.

它的作用基本上是删除所有的32并检查结果字符串是否为空,这表明它在ascii代码中充满了空格。