Match a line break on the line before another match
Question:
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:
[rn]+.*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
([.*rn])(.*A.*)
Answers:
$[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)
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 = 1nB = 2nC = 3', 'A = 10nB = 20nC = 30n']
If you want the actual newline, use the same pattern with re.search()
or re.finditer()
to get the match object(s).
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:
[rn]+.*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
([.*rn])(.*A.*)
$[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)
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 = 1nB = 2nC = 3', 'A = 10nB = 20nC = 30n']
If you want the actual newline, use the same pattern with re.search()
or re.finditer()
to get the match object(s).