正则表达式删除字符前的所有文本 [英] regex to remove all text before a character

查看:182
本文介绍了正则表达式删除字符前的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以删除_"之前的所有字符?例如,将 3.04_somename.jpg 更改为 somename.jpg.

Is there an easy way to remove all chars before a "_"? For example, change 3.04_somename.jpg to somename.jpg.

任何关于在哪里学习编写正则表达式的建议也会很棒.我检查的大多数地方都很难学习.

Any suggestions for where to learn to write regex would be great too. Most places I check are hard to learn from.

推荐答案

^[^_]*_

将匹配所有文本直到第一个下划线.用空字符串替换它.

will match all text up to the first underscore. Replace that with the empty string.

例如,在 C# 中:

resultString = Regex.Replace(subjectString, 
    @"^   # Match start of string
    [^_]* # Match 0 or more characters except underscore
    _     # Match the underscore", "", RegexOptions.IgnorePatternWhitespace);

要学习正则表达式,请查看http://www.regular-expressions.info

For learning regexes, take a look at http://www.regular-expressions.info

这篇关于正则表达式删除字符前的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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