正则表达式得到一个不跟数字的字符 [英] Regex to get one character not followed with digit

查看:35
本文介绍了正则表达式得到一个不跟数字的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果内部没有int,如何获得第一个字符:

How can I get first character if not have int inside:

我需要查看所有具有'['且不带整数的位置.

I need to look all the place have '[' without integer after.

例如:

[abc] pass
[cxvjk234] pass
[123] fail

现在,我有这个:

((([[])([^0-9])))

它得到前2个字符,而我只需要一个.

It gets the first 2 characters while I need only one.

推荐答案

通常,要匹配某些不带数字的模式,您需要添加(?!\ d)/(?![0-9]) 表达式的否定前瞻:

In general, to match some pattern not followed with a digit, you need to add a (?!\d) / (?![0-9]) negative lookahead to the expression:

\[(?!\d)
\[(?![0-9])
  ^^^^^^^^^

请参见

See the regex demo. This matches any [ symbol that is not immediately followed with a digit.

您当前的正则表达式模式已充满捕获组,如果我们删除那些多余的组,它看起来像(\ []([^ 0-9])-它与[,然后是非ASCII数字的字符.

Your current regex pattern is overloaded with capturing groups, and if we remove those redundant ones, it looks like (\[)([^0-9]) - it matches a [ and then a char other than an ASCII digit.

您可以使用

(?<=\[)\D

或(如果您只希望将ASCII数字与模式唯一匹配)

or (if you want to only match the ASCII digits with the pattern only)

(?<=\[)[^0-9]

请参见 详细信息:

  • (?< = \ [)-需要一个 [(但不使用 [] char,即不返回作为匹配值的一部分)之前...
  • \ D / [^ 0-9] -一个非数字.注意:仅否定ASCII数字,可以将 \ D RegexOptions.ECMAScript 标志一起使用.
  • (?<=\[) - a positive lookbehind requiring a [ (but not consuming the [ char, i.e. not returning it as part of the match value) before...
  • \D / [^0-9] - a non-digit. NOTE: to only negate ASCII digits, you may use \D with the RegexOptions.ECMAScript flag.

这篇关于正则表达式得到一个不跟数字的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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