开发者

Help with regex to find numbers

I'm trying to find all single numbers (with the use of vim):

  • numbers at start of line
  • numbers at end of line
  • the number has to be followed and proceded by a non number
  • but may not be folowed or proceded with a "dot" and a number or a "," and a number.

this is correct

7

word7

7word

7.

.7

a,7

word7word

word 7 word

7-7

but not this

7.开发者_StackOverflow社区7

7,7

77

Can anyone help me and explain the regex?

EDIT:

may'be I've found it with the help of an answer below about atomic grouping. Vim does support it: \(\d\.\|\d\,\|\d\)\@<!\d\(\.\d\|\,\d\|\d\)\@!


You can try this:

\v%(\d+%(\.|,))@<!\d@<!\d+@>%(%(\.|,)\d)@!

Explanation:

  • \v turns very magic : no need of many backslashes
  • the % signs are optional (make groups in parentheses non-matching)
  • (\d+(\.|,)@<! : not preceded with digits then . or ,
  • \d@<! : not preceded with a digit (be sure we are at the first digit
  • \d+@> : consume all digits (@> ensures that, see :help /\@>)
  • ((\.|,)\d)@! : after that, no dot or comma followed by a digit.


Give this a whirl:

^(?!\d(\.|\,)?\d)(((\D*?)\d(\D*?))|(\d(\D*?)\d))$

And let me know if you'd like an explanation.


Try this one:

\(\d[\.,]\)\@<!\d\@<!\d\d\@!\([\.,]\d\)\@!

Explanation:

It looks for digits (\d) that are not preceeded by a '.' or ',' followed by a digit (\(\d[\.,]\)\@<!) or a single digit (\d\@<!), and is not followed by '.' or ',' followed by a digit (\([\.,]\d\)\@!) or a single digit (\d\@!).

This one is straight from my vim so it should work in yours.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜