Matching first character occurrence in a string
-
martes, 06 de marzo de 2012 23:37
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! :)
- Editado theITvideos martes, 06 de marzo de 2012 23:59
Todas las respuestas
-
miércoles, 07 de marzo de 2012 2:29
On 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*", "");
Ron- Marcado como respuesta Lie YouModerator lunes, 12 de marzo de 2012 5:40
-
martes, 13 de marzo de 2012 9:58
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.
- Editado Phape martes, 13 de marzo de 2012 9:59
-
jueves, 12 de abril de 2012 10:04Hello,
This might helps!
You can get regular expression from following link: (Search for the appropriate topic)
You can verify that expression on following link.:
Javascript Regular Expression Validator | Regular Expressions Library and Testing Tool

