1

I have a text that has a lot of \n and | in it. Here is a sample:

this is a sample\n text. This symbol | shows what I am talking about.
This is \n another | sample

I want to be able to extract everything that is between \n and |. For above example this is: text. This symbol as well as another How can I do that in Python 2.7?

1
  • is \n a literal character or newline character? Commented Jan 29, 2015 at 16:16

2 Answers 2

1

You can use:

s='this is a sample\n text. This symbol | shows what I am talking about.\nThis is \n another | sample'

>>> print re.findall(r'\n([^|\n]*)\|', s);
[' text. This symbol ', ' another ']

This regex captures literal \n followed by a negation pattern that says:

([^|\n]*) which means match 0 or more of any character that is NOT pipe or newline. Square brackets are used for capturing it in a group which will be printed later in findall output. It matches a literal | in the end.

Or else using lookaheads:

>>> print re.findall(r'(?<=\n )[^|\n]*(?= +\|)', s);
['text. This symbol', 'another']
  • (?<=\n ) is a lookbehind that means match should be preceded by newline and a space
  • (?= +\|) is a lookahead that means match should be followed by a space and pipe.
Sign up to request clarification or add additional context in comments.

Comments

1

Use capturing group.

re.findall(r'\n([^|]*)\|', string)

[^|]* matches any character but not of a | symbol, zero or more times. By default re.findall prints the characters which are present inside the capturing group. So it prints out the in-between characters. | is a special meta character in regex which acts like an alternation operator. To match a literal | symbol, you must need to escape it in your regex.

2 Comments

Thanks Avinash. Can you please explain how it works?
Actually this gives incorrect result for re.findall(r'\n([^|]*)\|', 'abc\n foo\n bar\n baz | 123') i.e. [' foo\n bar\n baz '] instead of [' baz ']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.