帮助使用C#中的正则表达式 [英] Help with regular expressions in C#

查看:57
本文介绍了帮助使用C#中的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我在一些字符串中有文字...

Hi guys!
I have text in some string ...

<br />
string m_TextToValidate;<br />



请以正则表达式为我提供帮助,该正则表达式可以搜索文本中的所有单词,即使有一个符号也是如此
排除所有换行符和空格....
例子,



please help me with a regular expression that searches for all words in text even with one symbol
excluding all linebreaks and whitespaces....
Example,

<br />
<pre><br />
"Lazy fox jumps over some                   stone      and I want to cheat that         fox"<br />
</pre><br />





<br />
  Lazy<br />
  fox<br />
  jumps<br />
  over <br />
  some<br />
  stone<br />
  and <br />
  I <br />
  want<br />
  to<br />
  cheat<br />
  that<br />
  fox<br />



这里有13个字...



13 words here ...

推荐答案

SA颇具吸引力,但做了一些细微调整-用"+"代替"*":
SA has it pretty spot on, with a minor tweak - replace the ''*'' with a ''+'':
//  using System.Text.RegularExpressions;

/// <summary>
///  Regular expression built for C# on: Thu, Mar 24, 2011, 07:26:23 PM
///  Using Expresso Version: 3.0.3634, http://www.ultrapico.com
///
///  A description of the regular expression:
///
///  \b\w+\b
///      First or last character in a word
///      Alphanumeric, one or more repetitions
///      First or last character in a word
///
///
/// </summary>
public static Regex regex = new Regex(
      "\\b\\w+\\b",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );


// This is the replacement string
public static string regexReplace =
      "<Hello>";


//// Replace the matched text in the InputText using the replacement pattern
// string result = regex.Replace(InputText,regexReplace);

//// Split the InputText wherever the regex matches
// string[] results = regex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = regex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = regex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = regex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = regex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = regex.GetGroupNumbers();



获取一份Expresso的副本:它为我生成了代码,并且还解释并帮助设计了正则表达式.



Get yourself a copy of Expresso: it generated the code for me, and also explains and helps design regexes.


也许在错误的轨道上,但您可以简单地使用
吗?
Maybe on the wrong track but could you use simply
\w+


只要您将所有非空白字符都视为有效的文字字符,以下是一个正则表达式即可处理:
Just in case you consider all non-whitespace characters valid word characters, here is a regular expression that will handle that:
((?!\s).)+


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

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