怎么样..?使用正则表达式搜索{n} char或{-n} ...?n} [英] how..? Search {n} char or {-n} using regex...?n}

查看:97
本文介绍了怎么样..?使用正则表达式搜索{n} char或{-n} ...?n}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



怎么匹配(n)或(-n)少然后n给定字符串中的字符数...



假设我有一个字符串,如>>

ABCDEFGHIJKLMNOP .... YZ



模式待搜索>>

LMNOPQ {-5} UVWX



i希望匹配n或更少n而不是char ...



如何获得...

提前致谢



任何建议将不胜感激.. 。

Hi,
how to match (n) or (-n) less Then n number of character within a given string ...

suppose i have one string like>>
ABCDEFGHIJKLMNOP....YZ

Pattern To be search>>
LMNOPQ{-5}UVWX

i want to match n or less then n no of char ...

How to Get That ...
Thanks in advance

Any Suggestions will be appreciated...

推荐答案

不要使用正则表达式 - 它们是文本匹配系统,而不是文本处理。他们没有任何将子字符串转换为数字的规定,因此你的正则表达式必须明确检查{1},{2},{3},... {999},这是荒谬的(并且容易出错)。 br />


相反,做一个找到数字的正则表达式 - 这很简单 - 然后在C#或VB中提取后面的子字符串。正则表达式返回的匹配对象包括匹配文本的索引和长度,因此在此之后提取子字符串在任何一种语言中都是微不足道的。
Don't do it with a regex - they are text matching systems, not text processing. They don;t have any provision for converting substrings to numbers, so your regex would have to explicitly check for {1}, {2}, {3}, ... {999} which is ridiculous (and error prone).

Instead, do a Regex which finds the number - that's trivial - and then extract the substring after it in C# or VB. The Match object that the regex returns includes the Index and Length of the matched text, so extracting the substring after that is trivial in either language.


这篇关于怎么样..?使用正则表达式搜索{n} char或{-n} ...?n}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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