locked
Matching first character occurrence in a string

    Pergunta

  • 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 terça-feira, 6 de março de 2012 23:59
    terça-feira, 6 de março de 2012 23:37

Respostas

  • 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 Resposta Lie YouModerator segunda-feira, 12 de março de 2012 05:40
    quarta-feira, 7 de março de 2012 02:29

Todas as Respostas

  • 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 Resposta Lie YouModerator segunda-feira, 12 de março de 2012 05:40
    quarta-feira, 7 de março de 2012 02:29
  • 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 terça-feira, 13 de março de 2012 09:59
    terça-feira, 13 de março de 2012 09:58
  • Hello,

    This might helps!

    You can get regular expression from following link: (Search for the appropriate topic)

    Regular Expression Library

    You can verify that expression on following link.:

    Javascript Regular Expression Validator | Regular Expressions Library and Testing Tool

    Cheers,

    quinta-feira, 12 de abril de 2012 10:04