English 中文(简体)
Regex 模式数字,然后有一个字符
原标题:Regex pattern numbers followed by a character
  • 时间:2012-05-28 14:14:41
  •  标签:
  • c#
  • regex

我试图在接下来的场景中 将雷盖斯写得正确,但遇到了一些麻烦。下面是场景。

我的绳子看起来是这样的:

"办公时间(h) 介于8小时到18小时之间。"

从上面的字符串我需要 "8h" 18h"

到目前为止,我做了这个 ""[0-9]*"。但是这让我有了 'h" , 8h" 18h"

外面的专家有什么想法吗?

问题回答

[0-9]**[h] 替换为 [0-9]+h

表示它必须一次或多次出现。 括号 h 中没有任何用处, 因为它单独存在 。

您也可以使用 d+h 来增加可读性( d 匹配任何数字)。

用+(加号)替换 *,即:

[0-9]+[h]

在正方言中说:

* 表示匹配任何前一个标记数( 输入零) 。

+ 表示与前一个标记的任意数相匹配

另外,h周围的方括号是多余的,因为您只匹配一个字符。

表示 Regex 中的零重复或更多重复。 请指定重复的corret number。

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ <\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

d 表示一位数。

指一两次重复( d )。

表示词的开始或结束(因为任何字母或数字不应在模式之前或遵循模式)。

它不会找到 "h" "123h" 18hertz"

您可以使用此正则 :

d+h




相关问题
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. ...