首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试字符串中是否所有字符都不是字母数字

测试字符串中是否所有字符都不是字母数字
EN

Stack Overflow用户
提问于 2019-03-10 13:16:13
回答 1查看 90关注 0票数 0

下面的字符串可能是错误API调用的结果:

代码语言:javascript
复制
_±êµÂ’¥÷“_¡“__‘_Ó ’¥Ï“ùü’ÄÛ“_« “_Ô“Ü“ù÷ “Ïã“_÷’¥Ï “µÏ“ÄÅ“ù÷ “Á¡ê±«“ùã ê¡Û“_ã “__’

我不确定哪些行包含非字母数字字符,我的任务是确定哪些行有问题。

另一个问题是,一些非字母数字字符与我仍希望保留和搜索的字符串一起出现,例如:

代码语言:javascript
复制
This sentence is fine and searchable, but a few non-alphanumeric äóî donäó»t popup

是否有一种方法可以测试字符串的全部内容是否是非字母数字的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-10 20:27:43

可以使用正则表达式查找仅包含标准字母和数字字符(包括逗号、句点、感叹号和问号以及空格)的所有行:

代码语言:javascript
复制
clear
input str168 var1
"_±êµÂ’¥÷“_¡“__‘_Ó ’¥Ï“ùü’ÄÛ“_« “_Ô“Ü“ù÷ “Ïã“_÷’¥Ï “µÏ“ÄÅ“ù÷ “Á¡ê±«“ùã ê¡Û“_ã “__’"
"This sentence is fine and searchable, but a few non unicode äóî donäó»t popup"                                                                                     
" This is a regular sentence of course"                                                                                                                                   
" another sentence, but with comma"                                                                                                                                       
" but what happens with question marks?"                                                                                                                                  
" or perhaps an exclamation mark!"                                                                                                                                       
end

generate tag = ustrregexm(var1, "^[A-Za-z0-9 ,.?!]*$")

. list tag, separator(0)

     +-----+
     | tag |
     |-----|
  1. |   0 |
  2. |   0 |
  3. |   1 |
  4. |   1 |
  5. |   1 |
  6. |   1 |
     +-----+

另一种可能是使用正则表达式来排除没有任何字母和数字字符的任何行,在这种情况下,这种解决方案涵盖了两种必需的情况:

代码语言:javascript
复制
clear
input str168 var1
"_±êµÂ’¥÷“_¡“__‘_Ó ’¥Ï“ùü’ÄÛ“_« “_Ô“Ü“ù÷ “Ïã“_÷’¥Ï “µÏ“ÄÅ“ù÷ “Á¡ê±«“ùã ê¡Û“_ã “__’"
"This sentence is fine and searchable, but a few non unicode äóî donäó»t popup"                                                                                     
" This is a regular sentence of course"                                                                                                                                   
" another sentence, but with comma"                                                                                                                                       
" but what happens with question marks?"                                                                                                                                  
" or perhaps an exclamantion mark!"                                                                                                                                       
"¥Ï“ùü’ÄÛ“_« “_Ô“Ü“ù÷ "                                                                                                                          
"¥Ï“ùü’ÄÛ hihuo"                                                                                                                                                
end

generate tag = ustrregexm(var1, "^[^A-Za-z0-9]*$")

list tag, separator(0)

     +-----+
     | tag |
     |-----|
  1. |   1 |
  2. |   0 |
  3. |   0 |
  4. |   0 |
  5. |   0 |
  6. |   0 |
  7. |   1 |
  8. |   0 |
     +-----+
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55084674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档