c尖锐的正则表达式替换任何单词的第一个或最后一个字符 [英] c sharp regex replace first or last character of any word

查看:133
本文介绍了c尖锐的正则表达式替换任何单词的第一个或最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想通过使用正则表达式删除任何单词的第一个和最后一个字符....

例如..

(HELLO)(WORLD)将返回=== HELLO WORLD

和(HE/LLO)(WO/RLD)将返回====== HE/LLO WO/RLD



或//Hello WOrld/将返回========= Hello WOrld

或(HELLO)(W/ORLD)STACKOVERFLOW)将返回=== HELLO W/ORLD STACKOVERFLOW

(He(Lo)Wo(R)ld将返回===== He(Lo Wo(R)ld

我想替换所有括号..我希望替换任何单词的第一个和最后一个括号.....

我正在尝试...

temp = Regex.Replace(temp,@"^ [!@#$%^&*()_ + = [{]}; ::<> | ./?,\''""-]+", ");

但是此正则表达式方程式仅在FOUND ...删除FIRSt CHaracter时使用. | ./?,\''""-]+,");

但是此正则表达式方程式只能删除FIRSt CHaracter IF FOUND....


为什么要使用正则表达式?

 // 我个人将其作为扩展方法
公共 静态 字符串 ReplaceOccurrence( 字符串 str,字符串目标,字符串替换, int  pos = -1;
     int  counted =  0 ;
    字符串 result = " ;
    
    {
        pos =(pos >  =  0 )? str.IndexOf(target,pos + 1):pos = str.IndexOf(target);
        如果(pos >  =  0 )
        {
            计数++;
        }
    } 同时(pos >  =  0  &&计数!=计数);
    如果(pos >  =  0 )
    {
        结果= str.Insert(pos,替换).Remove(pos +替换.Length,target.Length);
    }
    返回结果;
} 


用法:

 字符串 x = " ;
字符串 y = " ;

y = ReplaceOccurrence(x," "  ^"//  result ="^ hello)(世界)" 
y = ReplaceOccurrence(x," "  ^"//  result =(hello)^ world)" 
y = ReplaceOccurrence(x," "  ^"//  result =(你好)(world ^" 
y = ReplaceOccurrence(x," "  ZZZZ"//  result =(你好)ZZZZworld)" 
y = ReplaceOccurrence(x," " "//  result =(你好)(世界) 


want to remove first and last character of any word by using regex....

FOr example..

(HELLO) (WORLD) will return === HELLO WORLD

and (HE/LLO) (WO/RLD) will return ====== HE/LLO WO/RLD



or //Hello WOrld/ will return ========= Hello WOrld

or (HELLO) (W/ORLD) STACKOVERFLOW) will return === HELLO W/ORLD STACKOVERFLOW

(He(Lo) Wo(R)ld will return ===== He(Lo Wo(R)ld

I dun want to replace all parenthesis ..I wish to replace First and last parenthesis of any word.....

I m trying this ...

temp = Regex.Replace(temp, @"^[!@#$%^&*()_+=[{]};:<>|./?,\''""-]+", " ");

BUT this regex equation ONly remove FIRSt CHaracter IF FOund....

解决方案

%^&*()_+=[{]};:<>|./?,\''""-]+", " ");

BUT this regex equation ONly remove FIRSt CHaracter IF FOund....


Why use regex?

// I would personally make this an extension method
public static string ReplaceOccurrence(this string str, string target, string replacement, int count)
{
    int pos = -1;
    int counted = 0;
    string result = "";
    do
    {
        pos = (pos >= 0) ? str.IndexOf(target, pos+1) : pos = str.IndexOf(target);
        if (pos >= 0)
        {
            counted++;    
        }	
    } while (pos >= 0 && counted != count);
    if (pos >= 0)
    {
        result = str.Insert(pos, replacement).Remove(pos + replacement.Length, target.Length);
    }
    return result;
}


Usage:

string x = "(hello) (world)";
string y = "";

y = ReplaceOccurrence(x, "(", "^", 1); // result = "^hello) (world)"
y = ReplaceOccurrence(x, "(", "^", 2); // result = "(hello) ^world)"
y = ReplaceOccurrence(x, ")", "^", 2); // result = "(hello) (world^"
y = ReplaceOccurrence(x, "(", "ZZZZ", 2); // result = "(hello) ZZZZworld)"
y = ReplaceOccurrence(x, ")", "", 2); // result = "(hello) (world"


这篇关于c尖锐的正则表达式替换任何单词的第一个或最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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