简体   繁体   中英

Regular expression to replace with empty string

I require a regular expression which does following

  1. Match if first alphabet of first word in starting line is in lowercase .
  2. Starting from word containing above alphabet , replace with empty string until a word starting with uppercase occurs

This is what I tried:

string result = Regex.Replace(input,@"^[a-z]\s?[a-z0-9]\s?[^A-Z]","");

This is what should happen:

Sample input = "of !jgf area. The wealth of nation"

Required Output ="The Wealth of nation"

What should I do to improve/correct?

您可以像下面这样:

string result = Regex.Replace(input,@"^[^A-Z]*","");

This pattern will suffice.

^[^A-Z]+

It will replace all the characters that is not an upper case letter

By the way there is a much faster way without RegEx in C#.

int index=str.indexOfAny(new char[] { 'A', 'B', ..., 'Z'})
if(index!=-1){
    str = str.Substring(index);
}
" ([A-Z]+.*)"

使用组,Gruops [1]是您想要的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM