Tuesday, March 06, 2012 11:37 PM
I am trying to remove (or match) the first word in a string until a specific character (first coma) is found. For example:
apple, is a healthy, fruit
It should say:
is a healthy, fruit
it removed everything before the first coma occurance
I am new to regular expressions, what can i use to achieve this.
Thank you! :)
- Edited by theITvideos Tuesday, March 06, 2012 11:59 PM
Wednesday, March 07, 2012 2:29 AMOn Tue, 6 Mar 2012 23:37:14 +0000, theITvideos wrote:>>>Hi there,>>I am trying to remove (or match) the first word in a string until a specific character (first coma) is found. For example:>>apple, is a healthy, fruit>>It should say:>>is a healthy, fruit>>it removed everything before the first coma occurance>>>>I am new to regular expressions, what can i use to achieve this.>>Please reply.>>Thank you! :)>>You need to describe what you want more precisely.Your example does not match your request.In your request, you write you want to remove or match everything untl the first comma is found. However, in your example, you are also removing the <space> that follows that first comma.This may seem like nit-picking, but it is this kind of imprecision that can lead to expressions or programs that don't work quite the way they should.To match everything up to and including the first comma, you can use\A[^,]+,or^[^,]+,To also match any whitespace characters after that first comma, you will need to append a \s* to that regex.A line that might be used in a program C#, to replace that first word, might be:resultString = Regex.Replace(subjectString, @"\A[^,]+,\s*", "");
- Marked As Answer by Lie YouModerator Monday, March 12, 2012 5:40 AM
Tuesday, March 13, 2012 9:58 AM
I think this one is good for you.
string pattern = @"\w+(?=(,\s+))\1"; Regex r = new Regex(pattern); string input = @"apple, is a healthy, fruit"; string temp = r.Replace(input,"",1); Console.Write(temp);
The result is what you want.
Please Mark it as answer, if it helps solve your problem.
- Edited by Phape Tuesday, March 13, 2012 9:59 AM
Thursday, April 12, 2012 10:04 AMHello,
This might helps!
You can get regular expression from following link: (Search for the appropriate topic)
You can verify that expression on following link.: