regex - C# Replace Everything before the first Space -
i need remove in string before first occurrence of space.
- every string starts number , followed space
- replace number , space, leaving rest of string in tact
for example:
22 cats of india
4 royal highness
562 eating potatoes
42 biscuits in 2nd fridge
2564 niagara falls @ 2 pm
i need:
the cats of india
royal highness
eating potatoes
biscuits in 2nd fridge
niagara falls @ 2 pm
basically remove every number before first space, including first space.
i tried this:
foreach (string line in lines) { string newline = line.trim().remove(0, line.indexof(' ') + 1); } this works numbers below 10. after hits 2 digits, doesn't work properly.
how should change code?
if want make sure match digits @ beginning of string, can use following regex:
^\d+\p{zs} see demo
declare like:
public static readonly regex rx = new regex(@"^\d+\p{zs}", regexoptions.compiled); the ^\d+\p{zs} regex means: 1 or more digits @ start of string followed 1 whitespace.
and use like
string newline = rx.replace(line, string.empty); edit: make sure line has no leading whitespace, can add .trim() strip like:
regex rx = new regex(@"^\d+\p{zs}", regexoptions.compiled); string newline = rx.replace(line.trim(), string.empty);
Comments
Post a Comment