c#string找到数字的正则表达式 [英] c# string Regex that finds digits

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

问题描述

你好,我在使用正则表达式时遇到问题。

我有一行

Hello, I`m having problems with Regex.
I have a line

Contract Nr.123456,reg.Nr.654321-118





我想使用只查找字符串的正则表达式 123456 ,但没有找到第二个6位-3位数字符串( 654321-118



这是什么我提出了,但不知道接下来要做什么



I want to use a regex that Finds only the string 123456, but doesnt find the second 6 digit- 3 digit string(654321-118)

This is what i came up with, but dont really know what to do next

string regex4 = @"\d{4,6}[^-]";





任何想法?

谢谢。



Any Ideas?
Thank you.

推荐答案

如果你不知道下一步该怎么做,请访问这些链接:

如何:使用正则表达式搜索字符串(C#编程指南) [ ^ ]

C#Regex.Match示例 [ ^ ]

C#中的常规表达用法 [ ^ ]

30分钟正则表达式教程 [ ^ ]
If you don't know what to do next, please, visit these links:
How to: Search Strings Using Regular Expressions (C# Programming Guide)[^]
C# Regex.Match examples[^]
Regular Expressions Usage in C#[^]
The 30 Minute Regex Tutorial[^]


你好,



你需要看一下正则表达式x(?!y)



见这里:https://developer.mozilla.org / en-US / docs / Web / JavaScript / Reference / Global_Objects / RegExp [ ^ ]



例如x(?| y)表示匹配x但不是当它跟着y



所以在你的情况下:



匹配一个4到6的数字长字符串,但不是当它后面跟着一个0到2位的字符串,一个短划线和3位数字:



\d {4,6}(?!\\ \\ d {0,2} -\ {3})



当我遇到正则表达式时,我经常用来调试我的正则表达式的一个好资源是:

http://regexpal.com/ [ ^ ]



希望有所帮助。



Valery。
Hello,

You need to look at the regex pattern x(?!y)

see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp[^]

For example x(?|y) means "match x but not when it is followed by y"

So in your case:

Match a 4 to 6 number long string, but not when it is followed by a 0 to 2 digit string a dash and 3 digits:

\d{4,6}(?!\d{0,2}-\d{3})

When I am stuck with Regex a good resource I frequently use to debug my regex expressions is:
http://regexpal.com/[^]

Hope it helps.

Valery.


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

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