Regexp question
-
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 textand 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 textor:
a: a-related text
b: b-related text
some strange text
f: f-related textAny suggestion on how to write my expression? Kjetil
-
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 textand 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 textor:
a: a-related text
b: b-related text
some strange text
f: f-related textAny suggestion on how to write my expression? Kjetil
-
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 textand 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 textor:
a: a-related text
b: b-related text
some strange text
f: f-related textAny suggestion on how to write my expression? Kjetil
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.
-
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.
Beautiful expression. But how do I name my groups? Kjetil
-
Beautiful expression. But how do I name my groups? Kjetil
-
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.
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: Mexicanand 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
-
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: Mexicanand 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
-
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.
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: Mexicanand my match.Groups[0].Value is
Breakfast: Sliced Bread
Lunch: At McDonald'sinstead of
Breakfast: Sliced Bread
Lunch: At McDonald's
Dinner: Chicken
Supper: MexicanWhy? This is starting to be embarrassing :) Kjetil
-
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: Mexicanand my match.Groups[0].Value is
Breakfast: Sliced Bread
Lunch: At McDonald'sinstead of
Breakfast: Sliced Bread
Lunch: At McDonald's
Dinner: Chicken
Supper: MexicanWhy? This is starting to be embarrassing :) Kjetil
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.