使用正则表达式从字符串中删除文本(& 括号) [英] Remove text (& brackets) from a string with regex

查看:39
本文介绍了使用正则表达式从字符串中删除文本(& 括号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您理解时很容易...不幸的是,我不知道!如果您能指导我找到答案,我将不胜感激,谢谢.

It's easy when you understand...unfortunately, I don't! I will deeply appreciate you if you can guide me to the answer, thanks.

我想只使用正则表达式捕获一个字符串,但删除括号内的所有文本.例如

I want to capture a string, using just regex, but remove any text that's within brackets. e.g.

这是一个典型的字符串...

This is a typical string...

<td class="rc_entry_alt" >Mark Anthony (IRE)</td>

我可以很轻松地捕捉到Mark Anthony (IRE)".我目前正在使用...

I can capture "Mark Anthony (IRE)" very easily. I'm currently using...

/<td class="rc_entry(_alt)?" >.*<\/td>/

我想要的是删除(IRE)".注意第一个括号前的空格.我也想去掉这个此外,() 之间的文本会有所不同,例如USA、ITY、FR 等.它应该是这样的...

What i'd like is to remove the " (IRE)". Note the preceding space prior to the first bracket. I want to remove this too. Also, the text between the ( and ) will vary, e.g. USA, ITY, FR, etc. It should look like this...

Mark Anthony

我毫不怀疑它非常简单,但它却让我望而却步.感谢您的时间:)

I've no doubt it's very simple, and yet it eludes me. Thanks for your time :)

n.b.括号中的内容并不总是存在.有时我用我提到的原始代码得到了我想要的.

n.b. The stuff in brackets isn't always there. Sometimes I get what I want with the original code I mentioned.

推荐答案

好的,所以先删除 HTML,然后执行以下操作以删除 (...) 部分:

Okay, so remove the HTML first, then do something like this to remove the (...) part:

\s+\(.*?\)

如果您知道 (...) 部分是字符串中的最后一个部分(即它后面没有任何内容),您也可以使用它来检查它是否在末尾:

If you know the (...) part is the very last thing in the string (i.e. there's nothing after it), you can use this to check that it's at the end, too:

\s+\(.*?\)$

只需使用正则表达式查找和替换函数,找到上面的表达式,然后什么也不替换.

Just use a Regex find and replace function, find the expression above, and replace with nothing.

这篇关于使用正则表达式从字符串中删除文本(&amp; 括号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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