Home > Mobile >  Match a line break on the line before another match
Match a line break on the line before another match

Time:07-30

I have a file with the following pattern:

A = 1
B = 2
C = 3
A = 10
B = 20
C = 30

I would like match only the line break before A = *

My attempt would also matches A = * as well as the line break

Code:

[\r\n] .*A.*

But I only need the line break alone. I also understand the first A would get sacrificed as there is no like above it.

Is it possible to use lookbehind?

EDIT: My attempt works but leave 2 groups which I can just access group 2. I was hoping to get this done in only 1 group

([.*\r\n])(.*A.*)

CodePudding user response:

I presume you are trying to split the string into ABC groups. Look for a \n with a lookahead = 'A'

text = """\
A = 1
B = 2
C = 3
A = 10
B = 20
C = 30
"""

re.split(r"\n(?=A)", text)

Returns:

['A = 1\nB = 2\nC = 3', 'A = 10\nB = 20\nC = 30\n']

If you want the actual newline, use the same pattern with re.search() or re.finditer() to get the match object(s).

CodePudding user response:

$[\s] ^ works in Adobe brackets, every RegExp editor is different, but I would generally try using the $ (end of line) and ^ (beginning of line) combined with \s which is any whitespace including new lines to match what you want.

Try something like this:

Example

  • Related