Match part of a string until it reaches the end of the line (python regex)?

Simply use r"(?PA-Za-z\t . +) This will match ASCII letters, spaces, tabs or periods. It'll stop at the first character that's not included in the group - and newlines aren't (whereas they are included in s and because of that it's irrelevant whether multiline mode is turned on or off).

Simply use r"(?PA-Za-z\t . +)" This will match ASCII letters, spaces, tabs or periods. It'll stop at the first character that's not included in the group - and newlines aren't (whereas they are included in \s, and because of that it's irrelevant whether multiline mode is turned on or off).

Thanks...couldn't be any simpler! – sq1020 Sep 10 at 1:15.

You can enable multiline matching by passing re. MULTILINE as the second argument to re.compile(). However, there is a subtlety to watch out for: since the + quantifier is greedy, this regular expression will match as long a string as possible, so if the next line is made up of letters and whitespace, the regex might match more than one line ($ matches the end of any string).

There are three solutions to this: Change your regex so that, instead of matching any whitespace including newline (\s) your repeated character set does not match that newline. Change the quantifier to +? , the non-greedy ("minimal") version of +, so that it will match as short a string as possible and therefore stop at the first newline.

Change your code to first split the text up into an individual string for each line (using text. Split('\n').

Thanks for the solutions! The first one sounds the easiest to implement. Do you know specifically how I can specify I only want single spaces to be matched as opposed to any whitespace?

I tried the second solution but it only matches a single character. – sq1020 Sep 9 at 20:02 1 My bad, should have mentioned - for all these solutions, you should also include the $ (end of string) anchor at the end. That way, with solution 2, re will find the shortest string that matches the regex and goes up to the end of a line, which is what you want.

For solution 1, a space can be represented in a character set by a literal space - no escaping required (i.e. A-Za-z . ) – azernik Sep 9 at 21:56.

Look at the flags parameter at docs.python.org/library/re.html#module-c....

This is not helpful - enabling multiline mode will not solve his problem. – Tim Pietzcker Sep 9 at 22:06 Just answered his question how to enable multiline ... – rocksportrocker Sep 9 at 22:12 Right, and if someone asks you if he should hold his hammer upwards or downwards to drive in a screw, you wouldn't tell him to use a screwdriver instead :) – Tim Pietzcker Sep 10 at 8:38.

I need to separate the Suite number from the rest of the string and save it in another variable. I also need a copy of the street address without the suite number. What is a good way to do this?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions