本文整理汇总了C#中System.Text.RegularExpressions.Regex.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.ToString方法的具体用法?C# Regex.ToString怎么用?C# Regex.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.RegularExpressions.Regex
的用法示例。
在下文中一共展示了Regex.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegularExpressionValidator
/// <summary>
/// Constructor
/// </summary>
/// <param name="regex">Regular expression as Regex</param>
/// <param name="errorMessage">Error message to show when validation fails</param>
public RegularExpressionValidator(
Regex regex,
string errorMessage)
: base(errorMessage)
{
Expression = regex.ToString();
_regex = regex;
}
示例2: GetRegexParser
public static Parser<string> GetRegexParser(Regex expression)
{
return new Parser<string>
{
ExpectedInput = expression.ToString(),
Func = input =>
{
var trimmedInput = input.TrimStart();
var match = expression.Match(trimmedInput);
return match.Success && match.Index == 0
? new Result<string>
{
Output = match.Value,
Rest = trimmedInput.Substring(match.Index + match.Length)
}
: new Error<string>
{
Message = $"Expected match on '{expression.ToString()}', got '{input}'.",
Expected = expression.ToString(),
Actual = trimmedInput
};
}
};
}
示例3: Parse
/// <summary>
/// Default implementation.
/// Command format is "commandname /parameter:valueWithSpaceAllowed /parameter:valueWithSpaceAllowed"
/// Case is ignored
/// If command can not be parsed, null is returned.
/// </summary>
/// <example>
/// websearch /s:weather cupertino /take:10 /language:de
/// </example>
/// <param name="command"></param>
/// <returns></returns>
public IEnumerable<KeyValuePair<string, object>> Parse(string command)
{
var seperator1 = _notation == ParserNotation.Windows ? '/' : '-';
var seperator2 = _notation == ParserNotation.Windows ? ':' : '=';
// Helppage as default:
if (string.IsNullOrWhiteSpace(command))
{
command = $"ncommandsystem {seperator1}help{seperator2}true";
}
if (_systemCommands.Any(x => x == command?.Trim().Trim(seperator1).ToLower()))
{
command = $"ncommandsystem {seperator1}{command?.Trim().ToLower()}{seperator2}true";
}
// Detect flags (eg ' /enable' -> ' /enable:true')
command = command + $" {seperator1}";
while (true)
{
var result = new Regex($" ({seperator1}[a-z,A-Z]+)[ ]+{seperator1}", RegexOptions.IgnoreCase).Match(command); // , command + ":true /"
if (!result.Success) break;
command = command.Replace(result.ToString(), result.ToString().TrimEnd(seperator1).TrimEnd() + "{seperator2}true {seperator1}");
}
command = command.TrimEnd(seperator1).TrimEnd();
// Parse command
return StringCommandUtil.ParseCommand(command, "^[a-z,A-Z]+$", $"({seperator1}[a-z,A-Z]+{seperator2})", new [] { seperator1, seperator2 }, true);
}
示例4: AddToQueue
public void AddToQueue(string source, string destination)
{
// Detect if the file has already been added with the same destination.
if (queue.FirstOrDefault(f => f.FileToExtract == source && f.Destination == destination) != null)
{
return;
}
// Detect rar archives named *.part01.rar, *.part02.rar etc.
Regex regexPartRar = new Regex(@"part\d*.rar$", RegexOptions.IgnoreCase);
Match matchPartRar = regexPartRar.Match(source);
if (matchPartRar.Success)
{
string baseName = source.Substring(0, source.LastIndexOf(matchPartRar.Value));
regexPartRar = new Regex(baseName.Replace("\\", "\\\\") + regexPartRar.ToString(), RegexOptions.IgnoreCase);
IEnumerable<QueueItem> items = GetItems().Where(i => regexPartRar.Match(i.FileToExtract.ToLower()).Success).ToList();
if (items.FirstOrDefault(f => f.Destination == destination) != null)
{
return;
}
}
QueueItem item = new QueueItem() { FileToExtract = source, Destination = destination, ArchiveSize = GetArchiveSize(source) };
queue.Add(item);
totalSize += item.ArchiveSize;
OnStatusChanged(new ItemAdded(item, queue.Count));
}
示例5: AssertOutput
public void AssertOutput(Regex regex)
{
var allOutput = string.Join(Environment.NewLine, captured.Infos);
Assert.That(regex.IsMatch(allOutput),
string.Format("Output did not match: {0}. Output:\r\n{1}", regex.ToString(), allOutput) );
}
示例6: regexComment
public static string regexComment(string file)
{
//第一步先去掉多行注释,然后再去掉单行注释,最后将空格换行进行释删除
Regex singleLineComment = new Regex(@"//(.*)", RegexOptions.Compiled);//换行
Regex multiLineComment = new Regex(@"(?<!/)/\*([^*/]|\*(?!/)|/(?<!\*))*((?=\*/))(\*/)", RegexOptions.Compiled | RegexOptions.Multiline);
Regex htmlComment = new Regex(@"(<!--)(.+)(-->)",RegexOptions.Compiled|RegexOptions.Multiline);
Regex jspComment = new Regex(@"(<%--)(.+)(--%>)",RegexOptions.Multiline);
string s = "";
using (StreamReader sr=new StreamReader (file,Encoding.UTF8))
{
//替换多行注释
s = sr.ReadToEnd();
s = Regex.Replace(s, multiLineComment.ToString(), "");
s = Regex.Replace(s,singleLineComment.ToString(),"");
s = Regex.Replace(s, htmlComment.ToString(), "");
s = Regex.Replace(s, jspComment.ToString(), "");
s=s.Replace(" ", "");
s=s.Replace("\n","");
s = s.Replace("\r", "");
}
return s;
}
示例7: WriteBson
private void WriteBson(BsonWriter writer, Regex regex)
{
// Regular expression - The first cstring is the regex pattern, the second
// is the regex options string. Options are identified by characters, which
// must be stored in alphabetical order. Valid options are 'i' for case
// insensitive matching, 'm' for multiline matching, 'x' for verbose mode,
// 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode
// ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.
string options = null;
if (HasFlag(regex.Options, RegexOptions.IgnoreCase))
options += "i";
if (HasFlag(regex.Options, RegexOptions.Multiline))
options += "m";
if (HasFlag(regex.Options, RegexOptions.Singleline))
options += "s";
options += "u";
if (HasFlag(regex.Options, RegexOptions.ExplicitCapture))
options += "x";
writer.WriteRegex(regex.ToString(), options);
}
示例8: DeserializesARegex
public void DeserializesARegex()
{
var regex = new Regex("its over (\\d+?)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
var input = Serializer.Serialize(new { Regex = regex });
var o = Deserializer.Deserialize<Fatty>(input);
Assert.Equal(regex.ToString(), o.Regex.ToString());
Assert.Equal(regex.Options, o.Regex.Options);
}
示例9: SerializeRegex
private string SerializeRegex(Regex regex)
{
switch (this.RegexFormat)
{
case Json.RegexFormat.Create:
{
StringBuilder flags = new StringBuilder(2);
if (regex.Options.HasFlag(RegexOptions.IgnoreCase) == true)
{
flags.Append("i");
}
if (regex.Options.HasFlag(RegexOptions.Multiline) == true)
{
flags.Append("m");
}
if (flags.Length > 0)
{
return "new RegExp(\"" + regex.ToString() + "\",\"" + flags.ToString() + "\")";
}
else
{
return "new RegExp(\"" + regex.ToString() + "\")";
}
}
case Json.RegexFormat.Default:
{
StringBuilder sb = new StringBuilder("/");
sb.Append(regex.ToString());
sb.Append("/");
if (regex.Options.HasFlag(RegexOptions.IgnoreCase) == true)
{
sb.Append("i");
}
if (regex.Options.HasFlag(RegexOptions.Multiline) == true)
{
sb.Append("m");
}
return sb.ToString();
}
default:
{
throw new InvalidEnumArgumentException("JsonHelper.RegexFormat", (int)this.RegexFormat, typeof(Json.RegexFormat));
}
}
}
示例10: Instruction
public Instruction(string code)
{
var catchMatch= new Regex("catch start: ([0-9]+);");
var pcMatch = new Regex("^[0-9]+").Match(code);//.ToString();
if(pcMatch.Success)
{
pc = Convert.ToInt32(pcMatch.ToString());
instArgs = code.Replace(pcMatch.ToString()+".","").Split(null).ToList();
instArgs.RemoveAll(str => String.IsNullOrEmpty(str));
}else
{
// for exceptions
MatchCollection matches = catchMatch.Matches(code);
instArgs.Add("catch");
instArgs.Add(matches[0].Groups[1].Value);
}
}
示例11: WriteJson
private void WriteJson(JsonWriter writer, Regex regex)
{
writer.WriteStartObject();
writer.WritePropertyName("Pattern");
writer.WriteValue(regex.ToString());
writer.WritePropertyName("Options");
writer.WriteValue(regex.Options);
writer.WriteEndObject();
}
示例12: WithRegexValueAddsAttributeCorrectly
public void WithRegexValueAddsAttributeCorrectly()
{
Regex value = new Regex( "Regex" );
HtmlAttributeBuilder builder = new HtmlAttributeBuilder();
var result = builder.Pattern( value );
Assert.AreSame( builder, result );
Assert.AreEqual( value.ToString(), builder.Attributes[ HtmlAttributes.Pattern ] );
}
示例13: Add
public VerbalExpressions Add(string value)
{
_source = _source != null ? _source + value : value;
if (_source != null)
{
p = new Regex(_prefixes + _source + _suffixes, RegexOptions.Multiline);
_pattern = p.ToString();
}
return this;
}
示例14: RegisterVariable
/// <summary>
/// Registers a new mapping between a regular expression and a value generator.
/// </summary>
/// <param name="regex">The regular expression to find out a variable.</param>
/// <param name="procedure">The value generator.</param>
/// <param name="item">The autocomplete item.</param>
public void RegisterVariable(Regex regex, Func<Match, object> procedure, RegexAutoCompleteItem item)
{
if (regex == null) throw new ArgumentNullException("regex");
if (procedure == null) throw new ArgumentNullException("procedure");
if (item == null) throw new ArgumentNullException("item");
Logger.Verbose("Registered the specified pattern '{0}'.", regex.ToString());
evaluators[regex] = procedure;
Register(item);
}
示例15: CreateHeaderSeparator_TrueRegex_ReturnTrueRegex
public void CreateHeaderSeparator_TrueRegex_ReturnTrueRegex()
{
//Arrange
var expected = new Regex("^$");
//Act
var actual = MethodTestHelper.RunInstanceMethod<DbRecorderBase, Regex>("CreateHeaderSeparator", _dbRecorderBase, new object[] { });
//Assert
Assert.AreEqual(actual.ToString(), expected.ToString());
}