Regular Uppercase Substitution in C #

I have the following C #, which simply replaces the parts of the input string that look like EQUIP: 19d005 with urls, for example:

input = Regex.Replace(input, @"(EQUIP:)(\S+)", @"<a title=""View equipment item $2"" href=""/EquipmentDisplay.asp?eqnum=$2"">$1$2</a>", RegexOptions.IgnoreCase);

As a result, the HTML is as follows.

<a title="View equipment item 19d005" href="/EquipmentDisplay.asp?eqnum=19d005">EQUIP:19d005</a>

The only problem is that the landing page expects the eqnum querystring to be fully UPPERCASE so that it returns the correct hardware when eqnum = 19D005, but fails if it gets eqnum = 19d005.

I suppose I can modify and correct the incorrect EquipmentDisplay.asp requirement of the header values, if possible, I would like to make the C # code compatible with the existing classic ASP page, the top of $ 2 in the Regex.Replace expression above.

Ideally, I would like HTML to return like this:

<a title="View equipment item 19d005" href="/EquipmentDisplay.asp?eqnum=19d005">EQUIP:19d005</a>

, EQUIP: 19d005 ( ), eqnum =.

, , ?

+5
6

, 2 , :

input = Regex.Replace(input, @"(EQUIP:)(\S+)", m => string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", m.Groups[1].Value, m.Groups[2].Value, m.Groups[2].Value.ToUpper()), RegexOptions.IgnoreCase);

, :

var input = Regex.Replace(input, @"(EQUIP:)(\S+)", Evaluator, RegexOptions.IgnoreCase);

private static string Evaluator(Match match)
{
    return string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", match.Groups[1].Value, match.Groups[2].Value, match.Groups[2].Value.ToUpper());
}
+12

Regex.Replace , , . , .

var match = Regex.Match(input, @"(EQUIP:)(\S+)", RegexOptions.IgnoreCase);
var input = String.Format( @"&lt;a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}""&gt;{0}{1}&lt;/a&gt;", 
match.Groups[1].Value,
match.Groups[2].Value,
match.Groups[2].Value.ToUpper());
+3

MatchEvaluator. , .NET. "" :

 static void Main(string[] args)
 {
     string input = "EQUIP:12312dd23";
     string output = Regex.Replace(input, @"(EQUIP:)(\S+)", 
         new MatchEvaluator(genURL), RegexOptions.IgnoreCase);
     Console.WriteLine(output);
     Console.ReadKey();
 }
 static string genURL(Match m)
 {
     return string.Format(@"<a title=""View item {0}"" 
            href=""/EqDisp.asp?eq={2}"">{1}{0}</a>",
            m.Groups[2].Value,m.Groups[1].Value,m.Groups[2].Value.ToUpper());
 }
+1

, :

input = Regex.Replace(input.ToUpper, @"(EQUIP:)(\S+)", @"<a title=""View equipment item $2"" href=""/EquipmentDisplay.asp?eqnum=$2"">$1$2</a>", RegexOptions.IgnoreCase);

Changing the case of a string is not something of a regular expression.

0
source
string input = "EQUIP:19d005";
Regex regex = new Regex (@"(EQUIP:)(\S+)", RegexOptions.IgnoreCase);
string eqlabel = regex.Replace(input, "$1");
string eqnum = regex.Replace(input, "$2");
string eqnumu = eqnum.ToUpperInvariant();
input = string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", eqlabel, eqnum, eqnumu);
0
source
public static string FormatToCustomAnchorTag(this string value)
{

    return Regex.Replace(value.ToLower() + value.ToUpper(),
                @"(?<equiplo>equip:)(?<equipnolo>\S+)(?<equipup>EQUIP:)(?<equipnoup>\S+)",
                @"<a title=""View equipment item ${equipnolo}"" href=""/EquipmentDisplay.asp?eqnum=${equipnoup}"">${equipup}${equipnolo}</a>");
}
0
source

All Articles