正则表达式:读取数字后面的字符
我有正则表达式代码:
<script type="text/javascript">
var str = "kw-xr55und";
var patt1 = /[T|EE|EJU].*D/i;
document.write(str.match(patt1));
</script>
它可以读取:
str= "KD-R55UND" -> as UND
但如果我输入:
str= "kw-tc800h2und -> result tc-800h2und. //it makes script read T in front of 800
i want the result as UND
如何使代码仅检查 800 后面的字符?
编辑
此代码后它可以工作:
<script type="text/javascript">
var str = "kw-tc800h2und";
var patt1 = /[EJTUG|]\D*D/i;
document.write(str.match(patt1));
</script>
但显示下一个问题,我可以显示结果,如果:
str= "kw-tc800un2d"
i want result like -> UN2D
i have regex code:
<script type="text/javascript">
var str = "kw-xr55und";
var patt1 = /[T|EE|EJU].*D/i;
document.write(str.match(patt1));
</script>
it can read:
str= "KD-R55UND" -> as UND
but if i type:
str= "kw-tc800h2und -> result tc-800h2und. //it makes script read T in front of 800
i want the result as UND
how to make the code just check at character behind the 800?
EDIT
After this code it can work:
<script type="text/javascript">
var str = "kw-tc800h2und";
var patt1 = /[EJTUG|]\D*D/i;
document.write(str.match(patt1));
</script>
but show next problem, i can show the result if:
str= "kw-tc800un2d"
i want result like -> UN2D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
它将匹配以
T
、EE
或EJU
开头并在字符串末尾结束的非数字字符序列。如果字符串必须像示例中那样以D
结尾,您可以将其添加到:如果您想在任何地方匹配它,而不仅仅是在字符串末尾,请尝试以下操作:
编辑:哎呀!不,这当然行不通。我试图猜测
[T|EE|EJU]
的意思,因为它是一个与一个字符E
匹配的字符类,J
、T
、U
或|
(相当于[EJTU|]
) ,我确信这不是你的意思。但到底是什么,试试这个:我仍然不明白你想做什么,但有时尝试和错误是前进的唯一方法。至少这次我测试了! :P
编辑:好的,所以匹配可以包含数字,只是不能以一开头。试试这个:
Try this:
It will match a sequence of non-digit characters starting with
T
,EE
orEJU
, and finishing at the end of the string. If the string has to end withD
as in your examples, you can add that in:If you want to match it anywhere, not just at the end of the string, try this:
EDIT: Oops! No, of course that doesn't work. I tried to guess what you meant by
[T|EE|EJU]
, because it's a character class that matches one of the charactersE
,J
,T
,U
or|
(equivalent to[EJTU|]
), and I was sure that couldn't be what you meant. But what the heck, try this:I still don't understand what you're trying to do, but sometimes trial and error is the only way to move ahead. At least I tested it this time! :P
EDIT: Okay, so the match can contain digits, it just can't start with one. Try this:
对于 PCRE,请尝试以下操作:
它使用后向查找来查找紧随数字之后的一组非数字字符。
对于 Javascript,请尝试以下操作:
它将匹配从文本末尾向后算起的所有非数字字符。
For PCRE, try this:
It uses a lookbehind to find a set of non-digit characters that comes immediately after a digit.
For Javascript, try this:
It will match any characters that are not digits from the end of the text backwards.
根据您使用的正则表达式的风格,您可以使用断言背后的外观。它本质上会说“如果它就在数字后面,则匹配它”。
在 python 中,它是这样的:
(?<=\d)\D*
哦,还有,正则表达式区分大小写,除非你设置不区分大小写。
Depending on what flavor of regex you're using, you can use a look behind assertion. It will essentially say 'match this if it's right after a number'.
In python it's like this:
(?<=\d)\D*
Oh, also, regex is case sensitive unless you set it not to be.
如果其中可以有 1 位数字,但不能有 2 位数字,请尝试
/[TEUJ]\D*\d*\D*D$/i
。获取更具体的信息将需要额外的信息,例如字符串的最大长度,或者解析tc800h2und
和h2und
之间的具体区别。Try
/[TEUJ]\D*\d*\D*D$/i
if it can have 1 digit in it, but not 2. Getting any more specific would require additional information, such as the maximum length of the string, or what exactly differs between parsingtc800h2und
andh2und
.