Matching first character occurrence in a string
-
2012年3月6日 下午 11: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! :)
- 已編輯 theITvideos 2012年3月6日 下午 11:59
所有回覆
-
2012年3月7日 上午 02: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- 已標示為解答 Lie YouModerator 2012年3月12日 上午 05:40
-
2012年3月13日 上午 09: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.
- 已編輯 Phape 2012年3月13日 上午 09:59
-
2012年4月12日 上午 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

