c sharp regex替换任何单词的第一个或最后一个字符(c sharp regex replace first or last character of any word)

   IT问题网   2021-01-22 00:00:00

问 题

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



例如..



(hello)(world)将返回=== hello world



和(he / llo)(wo / rld)将返回== ==== he / llo wo / rld







或// hello world /将返回========= hello world



或(hello)(w / orld)stackoverflow)将返回=== hello w / orld stackoverflow



(何(lo)wo(r)ld将返回=====他(lo wo(r)ld


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



我试试这个...



temp = regex.replace(temp,@"^ [!@#$%^*()_ + = [{]};: lt;gt; | ./ , \''"" - ] +","");



但这个正则表达式只能删除first character if found ....

解决方案

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



但这个正则表达式只能删除first character if found ....

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



  //  我个人认为这是一种扩展方法 
public static string replaceoccurrence( this string str, string target, string replacement, int count)
{
int pos = -1;
int 计算= 0 ;
string result = " ";
执行
{
pos =(pos gt; = 0 )? str.indexof(target,pos + 1):pos = str.indexof(target);
if (pos gt; = 0
{
count ++;
}
} while (pos gt; = 0 amp;amp;!; count!= count);
if (pos gt; = 0
{
result = str.insert(pos,replacement).remove(pos + replacement.length,target.length);
}
返回结果;
}


用法:



  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"

标签:替换替代任何单词一个或者最后字符



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

用于检查字符串数组是否为空的代码(Code for checking whether string array is empty or not)

problem hi, please send me the code for checking whether ...

消息在try catch块中不起作用(Message is not working in try catch block)

problem showing a modal dialog box or form wh ...

循环如何从开始日期算起5天(How loop to count for 5 days from the start date)

problem hi i have a starting date, now i nee ...

如何绑定diffirent windows窗体中使用的相同用户控件组合框的两个数据源(how to bind two data sources for same user control combo box used in diffirent windows forms)

problem lt;pre lang="c#"gt;lt;/pregt; i want to bind two ...

是他们的任何querybuilder工具(Is Their any querybuilder tool)

problem i want to know that is dere any gui q ...

我希望从gridview获得一个链接来搜索报告页面(i want to have a link from a gridview that will search a report page)

problem can someone help me i want to search fromgridview ...

存储文本框值时无法获得正确的输出(not getting correct output when storing textbox values)

problem i keep getting the output in messagebox as ''0'' ...

如何在asp.net中调用客户端的服务器端功能?(How to call server side function in client side in asp.net)

problem how to call server side function in client side i ...

将Excel数据导入并更新到sql中(Import and Update Excel data into sql)

problem hi, am working on a project which import data int ...

如何在asp中使用我的sql(How to use my sql in asp)

problem hi sir/madam, in our application of asp,we have ...

如何制作wpf鼠标事件?(How can made wpf mouse event )

problem how can made wpf mouse event see more: c# please ...

如何在包含三个用于用户输入的asp.net服务器控件的表中动态添加行(how to add rows dynamically in the table containing three asp.net server controls for user input)

problem hi a table having 5 rows and 3 columns, each ro ...

如何将string []数组值分配给cookie(How to assign a string[] array values to a cookie)

problem i have a string array and it is filling by databa ...

在基本MSI项目中从InstallShield显示消息“安装程序需要64位处理器”(Show message “Setup requires a 64Bit processor” from InstallShield in basic MSI project)

problem i am using installshield 11 and creat ...

c ++编程语言(c++ programming language)

problem hello all, i am looking to design c++ code for is ...

MySql DB(如何在部署时添加DB)(MySql DB(how to add DB at the time of deployment))

problem hello, i am going to deploy(c#.net with mysql 5.0 ...

如何为15个链接按钮单击事件实现相同的代码(How to implement same code for 15 linkbutton click events)

problem hi, i want to bind a tree in horizon ...

在数据网格中更改字段的名称(change field's name in a datagrid)

problem how can i change name of the each header columns ...

使用多个mdi表单(working with mutiple mdi forms)

problem hello i have a c sharp application, which contai ...

更改Html对象Zindex(Change Html Object Zindex)

problem hi all i have an object in my page. i ...