使用正则表达式仅选择字母数字单词 [英] Choosing just the alphanumeric words with regex

查看:60
本文介绍了使用正则表达式仅选择字母数字单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找正则表达式以仅从字符串中查找字母数字单词,即由字母或数字组合而成的单词.如果一个单词是纯数字或纯字符,我需要丢弃它.

I'm trying to find the regular expression to find just the alphanumeric words from a string i.e the words that are a combination of alphabets or numbers. If a word is pure numbers or pure characters I need to discard it.

推荐答案

试试这个正则表达式:

\b([a-z]+[0-9]+[a-z0-9]*|[0-9]+[a-z]+[a-z0-9]*)\b

或更紧凑:

\b([a-z]+[0-9]+|[0-9]+[a-z]+)[a-z0-9]*\b

这将匹配所有以一个或多个字母开头且后跟一个或多个数字或反之亦然可能后跟一个或多个字母或数字的单词(注意单词边界 \b).所以总是满足至少一个字母和至少一位数字的条件.

This matches all words (note the word boundaries \b) that either start with one or more letters followed by one or more digits or vice versa that may be followed by one or more letters or digits. So the condition of at least one letter and at least one digit is always fulfilled.

这篇关于使用正则表达式仅选择字母数字单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆