-1

How to write a regexp to match such groups(in JAVA):

START s1 8 a  

a a b ebd 

END


START any character END

START END

I have tried:

START(.*)END

final Matcher matcher = Pattern.compile("START(.*)END").matcher(versions);

But multi lines START END blocks aren't being matched.

Vardan Hovhannisyan
  • 1,101
  • 3
  • 17
  • 40
  • What is the programming language you are using? You need to add some flag that mentions that `.` should also match new lines. – Anis R. Nov 14 '19 at 16:12
  • In which environment are you doing this? Bash, Python, Perl, ...? – mrhd Nov 14 '19 at 16:12

2 Answers2

3

Make the seach not greedy:

START(.*?)END

and don't forget the flag Pattern.DOTALL

Toto
  • 89,455
  • 62
  • 89
  • 125
1

In your pattern, you should specify a flag that makes . also match new lines:

Pattern.compile("START(.*?)END", Pattern.DOTALL);

Edit: Also, as Carlos pointed out in the comments, you also should make your regex not greedy (replace .* by .*?).

Anis R.
  • 6,656
  • 2
  • 15
  • 37
  • `.*` is greedy, that is, will try to match as much as possible, in this case up to the **last** `END, probably the whole input – user85421 Nov 14 '19 at 16:19
  • Right, answer updated. – Anis R. Nov 14 '19 at 16:22
  • It isn't specified in the question, but I would also add a warning about the fact that nested START...END blocks can't be properly matched by regular expressions. – QuentinC Nov 14 '19 at 16:39