简体   繁体   中英

Regular Expression C#

I need use c# RegEx to process a string. The idea is delete initials characteres, like a substring.

Example string := "04|aH 800 A574a CR";

The result should be "H 800 A574a CR".

But the patron is variable, because the string can be "4|aProtestas populares" and the result should be "Protestas populares"

Summarizing, I need the substring after a "|x" ignoring what is before this expression.

Thanks...

PD: Sorry by my english :S

var stringNew = Regex.Replace(stringOld, @"^.*?\|.", "");

This removes everything

  • at the start of the string ^
  • containing anything .*?
  • ending in a pipe |
  • plus any one character that follows .

If the string always has the pipe and one char after it, then what you want to keep, you can just use String.IndexOf() . Using one string method would be much faster then using a RegEx Object.

string str = "04|aH 800 A574a C.R.";
int nIndex = str.IndexOf('|') + 2;
string substr = str.Substring(nIndex); // contains "H 800 A574a C.R."

This Regex gives you desired result :

(\|)[a-zA-Z](?<wanted>[\s\S]+)$

Group named 'wanted' gives the after "|x".

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