0

I have this text

XXX
text 
XXX

XXX
text 
XXX

XXX
text 
XXX

and i want to capture the text between the XXX and XXX. (i am trying to get chapters out from a book )

 /XXX.*XXX/

This will capture the first begin and the last end

 /XXX.*?XXX/

This will skip every second chapter

Thanks ahead Barak

1
  • 2
    what regexp flavour/tool are you using? Commented Sep 9, 2011 at 11:29

4 Answers 4

4

If you text contains line feeds (\n) you'll need to add the "dot matched newline" switch to your regex, as well as making your match "non greedy":

/(?s)XXX.*?XXX/

Edited: Thanks to Alan's comment - I had the wrong switch: (?s) is correct

Sign up to request clarification or add additional context in comments.

2 Comments

Multiline mode alters the behavior of the anchors, ^ and $, so it would be useless here. Perhaps you're thinking of "single-line" or "DOTALL" mode, which enables the . to match linefeeds.
To work with reg-exes, you must know reg-exes. To code, you just know how to code. :)
2

Solution using sed

$ sed -n '/XXX/,/XXX/{n;p}' text
text 

text 

text 

Comments

1

If this XXX strings are always in separate lines, i would suggest simple iterating through lines and picking it 'by hand'. It should be faster than multi-line regexp.

python :

delim = "XXX"
inside = False
lines = []
idx = 0
for line in file:
    if line.strip() == delim:
        inside = not inside
        if inside: lines.append([])
        else: idx += 1
    elif inside:
        lines[idx].append(line)

Comments

0

Your description doesn't really match your examples. If XXX is supposed to represent a chapter heading, there would only be one at the beginning of each chapter. To detect the end of a chapter, you would need to do a lookahead for the next chapter heading:

/XXX.*?(?=XXX)/s

That should work for all but the last chapter; to match that you can use \z, the end anchor:

/XXX.*?(?=XXX|\z)/s

It really would help if we knew which regex flavor you're using. For example, in Ruby you would have to use /m instead of /s to allow . to match linefeeds.

Comments

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.