Kevin_ALA
Kevin_ALA

Reputation: 233

Match a line break on the line before another match

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.*)

Upvotes: 0

Views: 96

Answers (2)

richlowe
richlowe

Reputation: 1

$[\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.

Edit:

Try this for the lookahead:

($\s*)?^(?=A)

Upvotes: 0

RootTwo
RootTwo

Reputation: 4418

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).

Upvotes: 2

Related Questions