常规表达式问题
我有这样的字符串 <>1 <>2 <>3
我想删除所有“<>”以及“<>”之后的符号我想用这样的表达式替换 www.test.com/1.jpg, www.test.com/2.jpg, www.test.com/3.jpg
是否可以使用正则表达式?我只知道找到 '/<>.?/'
i'v got such string <>1 <>2 <>3
i want remove all '<>' and symbols after '<>' i want replace with such expression like www.test.com/1.jpg, www.test.com/2.jpg, www.test.com/3.jpg
is it possible to do with regex? i only know to find '/<>.?/'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
(假设您替换的元素只是数字。如果它们更通用,您需要将 '\d+' 替换为其他内容)。
(assuming your replaced elements are just numbers. If they are more general, you'll need to replace '\d+' by something else).
根据您的示例,我认为您根本不需要正则表达式。
Based on your example, I don’t think you need regex at all.
正则表达式允许您以您想要的任何方式操作字符串,以您想要的方式修改字符串,您可以使用以下正则表达式:
<>(\d)
并且您可以使用正则表达式反向引用来保留您拥有的值在分组括号中捕获,在本例中为单个数字。向后引用通常由 $ 符号表示,然后是您所引用的组的编号。如下所示:
www.test.com/$1
这将在正则表达式替换场景中使用,该场景将根据您实现正则表达式替换方法所用的语言以不同的方式实现。
Regex's allow you to manipulate a string in any fashion you desire, to modify the string in the fashion you desire you would use the following regex:
<>(\d)
and you would use regex back referencing to keep the values you have captured in your grouping brackets, in this case a single digit. The back reference is typically signified by the $ symbol and then the number of the group you are referencing. As follows:
www.test.com/$1
this would be used in a regex replace scenario which would be implemented in different ways depending on the language you are implementing your regex replace method in.