English 中文(简体)
如何获得数字
原标题:How to get numbers
  • 时间:2024-04-26 02:44:15
  •  标签:
  • c#
  • regex
myPattern = @"?:[.]?[d]+(?:[,.]*d)*";

然而,我要排除美元签字后的数字,并想排除括号中的数字()}[]。

例如:

input text (match)
-----------------------------
abc123,000.5xyz (match:123,000.5)
abc.5xyz (match:.5)
abc0.2xyz  (match:0.2)
abc123.2xyz (match:123.2)
abc$123xyz (false)
abc(123)77xyz (match:77)
abc12{123}xyz (match:12)
abc[123]xyz (false)
问题回答

您可使用这一规章:

(?<![${([d])d+(,d{3})*(.d+)?(?![]})])

这与:

  • (?<![${([d]) : negative lookbehind for $, (, [, { or a digit (the latter prevents matching from the middle of a sequence of digits)
  • d+(,d{3})*(.d+)? : regex to match a number with optional thousands and decimal parts
  • (?![]})]) : negative lookahead for ], }, or )

Regex demo on regex101

最容易的是,放松对你的要求,并用零宽的说法:

(?<![[({$])((?:d+(?:[,.]d+)?)|(?:[,.]d+))

That will capture all numbers NOT preceded by [({$, ie. we don t check if they are inside a full set of brackets/parentheses.

  • (?<![[({$]) will do the check for the preceding char
  • (?:d+(?:[,.]d+)?) numbers with xxx.xxx or just xxx
  • (?:[,.]d+) numbers with .xxx




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签