1

I would like to get the text between certain keywords [en] and [ja]

So for the following example:

[en]
Text
- Example
- Example
- Example

[ja]
Text
 - 例
 - 例
 - 例

I need it to return only:

Text
- Example
- Example
- Example

I have tried using regex:

([en])(.|\n)+?([ja])

But it only grabs the first 2 characters of first line. What am I doing wrong here?

2
  • @anubhava but it also gets [en] and [ja] with it Commented Jul 6, 2022 at 14:40
  • Ok then \[en]\n((?:.*\n)+?)\[ja] Commented Jul 6, 2022 at 14:41

2 Answers 2

1

Captures all the text between [en] and [ja]

(?<=\[en\]\n)(?:(?:.*\n)+?)(?=\n\[ja\])

Regex working link

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

6 Comments

Is there a way to skip the last line break?
yes you can use: (?<=[en]\n)(?:(?:.*\n)+?)(?=\n[ja])
that doesn't work
(?<=\[en]\n)(?:(?:.*\n)+?)(?=\n\[ja\]) does it
link and code edited on top. It is working.
|
1

You may use this regex for capturing text between [en] and [ja]:

\[en]\n((?:.*\n)*?)\n\[ja]

RegEx Demo

RegEx Details:

  • \[en]\n: Match [en] followed by a line break
  • ((?:.*\n)+?): Match anything followed by a line break. Repeat this group 1+ times (lazy matching) and capture matched text in group #1
  • \n\[ja]: Match line break followed by [ja]

2 Comments

Is there a way to skip the last line break?
@HeelMega: Try my updated answer now

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.