Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regexp question

Regexp question

Scheduled Pinned Locked Moved C#
questionhelptutorial
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kjetil Svendsen
    wrote on last edited by
    #1

    Hi. I have a text input like this:

    a: a-related text
    b: b-related text
    c: c-related text
    d: d-related text
    e: e-related text
    f: f-related text

    and a expression like this:

    (a\:\s*)(?.*)(\r\n)(b\:\s*)(?.*)(\r\n)(c\:\s*)(?.*)(\r\n)(d\:\s*)(?.*)(\r\n)(e\:\s*)(?.*)(\r\n)(f\:\s*)(?.*)(\r\n)

    So far so good. The problem is that I never know how many of the lines a-f which is present. The text might look like this:

    a: a-related text
    c: c-related text
    f: f-related text

    or:

    a: a-related text
    b: b-related text
    some strange text
    f: f-related text

    Any suggestion on how to write my expression? Kjetil

    L G 2 Replies Last reply
    0
    • K Kjetil Svendsen

      Hi. I have a text input like this:

      a: a-related text
      b: b-related text
      c: c-related text
      d: d-related text
      e: e-related text
      f: f-related text

      and a expression like this:

      (a\:\s*)(?.*)(\r\n)(b\:\s*)(?.*)(\r\n)(c\:\s*)(?.*)(\r\n)(d\:\s*)(?.*)(\r\n)(e\:\s*)(?.*)(\r\n)(f\:\s*)(?.*)(\r\n)

      So far so good. The problem is that I never know how many of the lines a-f which is present. The text might look like this:

      a: a-related text
      c: c-related text
      f: f-related text

      or:

      a: a-related text
      b: b-related text
      some strange text
      f: f-related text

      Any suggestion on how to write my expression? Kjetil

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Use multiline matching.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      1 Reply Last reply
      0
      • K Kjetil Svendsen

        Hi. I have a text input like this:

        a: a-related text
        b: b-related text
        c: c-related text
        d: d-related text
        e: e-related text
        f: f-related text

        and a expression like this:

        (a\:\s*)(?.*)(\r\n)(b\:\s*)(?.*)(\r\n)(c\:\s*)(?.*)(\r\n)(d\:\s*)(?.*)(\r\n)(e\:\s*)(?.*)(\r\n)(f\:\s*)(?.*)(\r\n)

        So far so good. The problem is that I never know how many of the lines a-f which is present. The text might look like this:

        a: a-related text
        c: c-related text
        f: f-related text

        or:

        a: a-related text
        b: b-related text
        some strange text
        f: f-related text

        Any suggestion on how to write my expression? Kjetil

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Don't write a pattern for matching each separate line, write a pattern that matches any of the lines that you want: (?:^|\r\n)([a-f]):\s*(.+?)(?:$|\r\n) This should give you a match for each line, containing the a-f character and the related text as captures.

        Despite everything, the person most likely to be fooling you next is yourself.

        K 1 Reply Last reply
        0
        • G Guffa

          Don't write a pattern for matching each separate line, write a pattern that matches any of the lines that you want: (?:^|\r\n)([a-f]):\s*(.+?)(?:$|\r\n) This should give you a match for each line, containing the a-f character and the related text as captures.

          Despite everything, the person most likely to be fooling you next is yourself.

          K Offline
          K Offline
          Kjetil Svendsen
          wrote on last edited by
          #4

          Beautiful expression. But how do I name my groups? Kjetil

          G 1 Reply Last reply
          0
          • K Kjetil Svendsen

            Beautiful expression. But how do I name my groups? Kjetil

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Without naming the groups, you can access them by index. You can name them like this: (?:^|\r\n)(?<section>[a-f]):\s*(?<description>.+?)(?:$|\r\n)

            Despite everything, the person most likely to be fooling you next is yourself.

            K 1 Reply Last reply
            0
            • G Guffa

              Without naming the groups, you can access them by index. You can name them like this: (?:^|\r\n)(?<section>[a-f]):\s*(?<description>.+?)(?:$|\r\n)

              Despite everything, the person most likely to be fooling you next is yourself.

              K Offline
              K Offline
              Kjetil Svendsen
              wrote on last edited by
              #6

              Hi. I really appreciate your help, but I still need some help. RegExp is not my strongest side :-) I need to rephrase my question. My text input is more like: (I'm not allowed to use actual data from my project)

              Breakfast: Sliced Bread
              Lunch: At McDonald's
              Dinner: Chicken
              Supper: Mexican

              and my task is to isolate what a person ate and save that info in my database. Different person might have different ways to report their meals. Each person has a consistent way though. There are ~150 persons and they all skip meals from time to time. Can you still help me ? Kjetil

              G 1 Reply Last reply
              0
              • K Kjetil Svendsen

                Hi. I really appreciate your help, but I still need some help. RegExp is not my strongest side :-) I need to rephrase my question. My text input is more like: (I'm not allowed to use actual data from my project)

                Breakfast: Sliced Bread
                Lunch: At McDonald's
                Dinner: Chicken
                Supper: Mexican

                and my task is to isolate what a person ate and save that info in my database. Different person might have different ways to report their meals. Each person has a consistent way though. There are ~150 persons and they all skip meals from time to time. Can you still help me ? Kjetil

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Instead of ([a-f]) in the pattern to match a single character, use (Breakfast|Lunch|Dinner|Supper) to match the strings.

                Despite everything, the person most likely to be fooling you next is yourself.

                K 1 Reply Last reply
                0
                • G Guffa

                  Instead of ([a-f]) in the pattern to match a single character, use (Breakfast|Lunch|Dinner|Supper) to match the strings.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  K Offline
                  K Offline
                  Kjetil Svendsen
                  wrote on last edited by
                  #8

                  Hi. It still does not work. New problem: The user input is like this

                  Breakfast: Sliced Bread
                  Lunch: At McDonald's
                  (had some coffee and snack)
                  Dinner: Chicken
                  Supper: Mexican

                  and my match.Groups[0].Value is

                  Breakfast: Sliced Bread
                  Lunch: At McDonald's

                  instead of

                  Breakfast: Sliced Bread
                  Lunch: At McDonald's
                  Dinner: Chicken
                  Supper: Mexican

                  Why? This is starting to be embarrassing :) Kjetil

                  G 1 Reply Last reply
                  0
                  • K Kjetil Svendsen

                    Hi. It still does not work. New problem: The user input is like this

                    Breakfast: Sliced Bread
                    Lunch: At McDonald's
                    (had some coffee and snack)
                    Dinner: Chicken
                    Supper: Mexican

                    and my match.Groups[0].Value is

                    Breakfast: Sliced Bread
                    Lunch: At McDonald's

                    instead of

                    Breakfast: Sliced Bread
                    Lunch: At McDonald's
                    Dinner: Chicken
                    Supper: Mexican

                    Why? This is starting to be embarrassing :) Kjetil

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    No, that is not correct. If you check your result again, you will see that you are getting the breakfast and the dinner. That is because the pattern is consuming the line break before and after each match, so the next line won't match. Change the part of the pattern that matches the end of the line or end of the string from (?:$|\r\n) to (?=$|\r\n) to make a zero-width positive lookahead.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups