17._5正则表达式的替换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace _17._5正则表达式的替换
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Welcome to ***ww****w*********************";
            string pattern = @"\bw{3}\.\w+\.(com|net|org)\b";
            string replacment ="\n"+@"http://$&";

            Console.WriteLine("替换前的字符串"+input);
            Console.WriteLine("替换后的字符串");
            Console.WriteLine(Regex.Replace(input,pattern,replacment));
            Console.WriteLine("替换后的字符串1:"+(Regex.Replace(input,pattern,replacment,RegexOptions.IgnoreCase)).ToLower());
            Console.WriteLine("替换后的字符串2:"+Regex.Replace(input,"www","\n"+@"http://www.",RegexOptions.IgnoreCase));

            Regex myregex = new Regex(pattern,RegexOptions.IgnoreCase);
            string result = myregex.Replace(input, replacment);
            Console.WriteLine("替换后的字符串3:"+result.ToLower());

            Console.ReadKey();
        }
    }
}
时间: 2024-08-24 23:27:19

17._5正则表达式的替换的相关文章

[转]使用DW正则表达式批量替换实例介绍

原文地址:http://www.jb51.net/Dreamweaver/86329.html 网站优化过程中,经常会遇到很多要批量修改的东西,一个个修改,明显效率低下.本文是使用DW正则表达式批量替换的一个实例 想法:想把<a href="#" target="_blank">BBBBBBB</a> 替换成 <a href="http://www.yourdomain.com/keyword.asp?=BBBBBBB&quo

sublime_中使用正则表达式查找替换

原文地址: http://frontmag.no/artikler/utvikling/regex-search-and-replace-sublime-text-2 我只是做下翻译(呃,简单理解下就好,我六级还没过呢) 正则的查找替换是st2内置的,不用装插件了 那,原文中是例子: 10x620x580mm 10x620x580mm 11x620x2390mm 11x620x2390mm 11x620x3050mm ... 作者的意图是想取得每行的第一个数字,然后加上mm,比如第一行,要变成1

Vim正则表达式匹配替换字符串

/********************************************************************** * Vim正则表达式匹配替换字符串 * 说明: * 用Vim用了几年了,平时也就替换字符串比较多,很少用正则表达式替换, * 今天在修改HTML文档的时候,不想一个一个去替换. * * 2018-6-1 深圳 宝安西乡 曾剑锋 ****************************************************************

正则表达式 replace()替换

定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp,replacement) 参数 描述 regexp 必需.规定了要替换的模式的 RegExp 对象.请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象. replacement 必需.一个字符串值.规定了替换文本或生成替换文本的函数. 返回值 一个新的字符串,是用 repl

eclipse使用技巧---使用正则表达式查找替换

1,Eclipse ctrl+f 打开查找框2,选中 Regular expressions (正则表达式) 去掉/* */(eclipse)        /\*(.|[\r\n])*?\*/去掉//(eclipse)             //.*$去掉import(eclipse)     import.*$去掉空行(eclipse)         ^\s*\n去掉空行(ue)               %[ ^t]++^p 把 <html.*property=/"([a-zA

01_正则表达式_03_替换

[替换] public String replaceAll( String regex,String replacement ); 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的 子字符串 . str.replaceAll(regex,repl)方法等同于 Pattern.compile(regex).matcher(str).replaceAll(repl); regex:用来匹配此字符串的正则表达式 replacement:用来替换每个匹配项的字符串 返回值为替换

js正则表达式/replace替换变量方法

转自:http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html 1. javascript 正则对象替换创建和用法:/pattern/flags 先简单案例学习认识下replace能干什么: 正则表达式构造函数: new RegExp("pattern"[,"flags"]); 正则表达式替换变量函数:stringObj.replace(RegExp,replace Text);参数说明:patt

Javascript正则表达式匹配替换

根据正则表达式的匹配结果将匹配项替换为*function regReplace(reg, str){ var result, //最终输出结果 out, //每次运行正则exec返回的匹配结果. index, //匹配项在字符串中位置 length; //匹配项长度 result = str.split(""); //将待匹配的字符串分开成数组,等待处理 while(out = reg.exec(str)){ //当匹配成功返回out数组,如果没有匹配项或达到字符串末尾返回null l

Java 正则表达式,替换图片,替换数字,和谐用语,复制文件

/** * indexOf("字符",位置int) //在方法中,只输入第一个属性默认从头开始查找属性中的字符,位置int表示从输入的int位置之后查找字符 * lastIndexOf("字符") //从字符串中,查找最后一个属性中的字符 * str="www.oracle.com"; //substring截取字符串 * str.substring(4,10) //从第四位截取到第10位 * str.subString(4) //从第四位截取到