Regex 101 Exercise I8 - replace space count with spaces
2006-02-15 20:03
405 查看
In this episode, Eric asks us to replace space count with spaces:
--------------------------------------------------------------------------------
Given a string with embedded space counts:
<15sp>Indented by 15 spaces
Replace the <<count>sp> with <count> spaces.
So, if you have
<4sp>Text
you should end up with
Text
----------------------------------------------------------------------------------
and my answer is:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
/*Regex 101 Exercise I8 - replace space count with spaces*/
namespace RegexExerciseI8
{
class Program
{
static void Main(String[] args)
{
Regex regex = new Regex(@"<(\d+)sp>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Console.WriteLine("Type in the text you want to process:");
String inputHtml = Console.ReadLine();
String resultHtml = regex.Replace(inputHtml, delegate(Match match)
{
return new String(' ', Int32.Parse(match.Groups[1].Value));
});
Console.WriteLine(resultHtml);
}
}
}
I think this exercise is very interesting and helpful, imagine that you are currently working on a text processing application, and you want the text to be formatted in a given way, i.e indentation, justfication, left alignment or right alignment etc, and you can add some formatting tags in the raw text, and use regular expression to match the individual formatting tags, and replace them with the predefined formats.
The possibility is endless.
--------------------------------------------------------------------------------
Given a string with embedded space counts:
<15sp>Indented by 15 spaces
Replace the <<count>sp> with <count> spaces.
So, if you have
<4sp>Text
you should end up with
Text
----------------------------------------------------------------------------------
and my answer is:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
/*Regex 101 Exercise I8 - replace space count with spaces*/
namespace RegexExerciseI8
{
class Program
{
static void Main(String[] args)
{
Regex regex = new Regex(@"<(\d+)sp>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Console.WriteLine("Type in the text you want to process:");
String inputHtml = Console.ReadLine();
String resultHtml = regex.Replace(inputHtml, delegate(Match match)
{
return new String(' ', Int32.Parse(match.Groups[1].Value));
});
Console.WriteLine(resultHtml);
}
}
}
I think this exercise is very interesting and helpful, imagine that you are currently working on a text processing application, and you want the text to be formatted in a given way, i.e indentation, justfication, left alignment or right alignment etc, and you can add some formatting tags in the raw text, and use regular expression to match the individual formatting tags, and replace them with the predefined formats.
The possibility is endless.
相关文章推荐
- Replace multiple spaces with one space in Python(替换多个空格为一个空格)
- T-SQL – Replace Multiple ExtraWhitespaces in a String with One Whitespace
- [转]Using Replacement Strings with Regex.Replace
- Write a method to replace all spaces in a string with ‘%20’.
- Replace all spaces in string with ' ' (Cracking the Code Interview)
- REPLACE Multiple Spaces with One
- Q1.4 Write a method to replace all spaces in a string with ‘%20’.
- 【待】1.4 Write a method to replace all spaces in a string with'%20'.
- Write a method to replace all spaces in a string with ‘%20’
- Regex 101 Exercise I9 - Count the number of matches
- 【5】Replace all spaces in a string with ‘%20’
- Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
- Replace the space in the string aa with "+"
- Keil 链接错误 Error: L6406E: No space in execution regions with .ANY selector matching
- 【代码积累】replace constructor with factory method
- python regex replace
- jQuery标签替换函数replaceWith()的使用例子
- [357]Count Numbers with Unique Digits
- jQuery replaceWith replaceAll end的用法
- Refactoring Day 31 : Replace conditional with Polymorphism