A
A
Alexander Mikhailov2014-01-17 13:10:06
Java
Alexander Mikhailov, 2014-01-17 13:10:06

How to create a regular expression in JAVA for a block of multiple lines?

There is the following text:

!re
=.id=*1
=.nextid=*2
=name=script1
=owner=admin
=policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api
=run-count =0
=source=
=invalid=true

You need to get the line:
script1

I do this:
Pattern p = Pattern.compile("^.*name=(.*)$", Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = p.matcher(str);
m.group(1) contains:
script1
=owner=admin
=policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api
=run-count=0
=source=
=invalid=true

If Pattern p = Pattern.compile("^.*name=(.*)$", Pattern.MULTILINE);
I don’t get anything ... Something I got confused at the end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bimeg, 2014-01-17
@chelaxe

The asterisk is needed not greedy in the second case.

Pattern p = Pattern.compile("^.*name=(.*?)$", Pattern.MULTILINE | Pattern.DOTALL);

A
Alexander Mikhailov, 2014-01-17
@chelaxe

Pattern p = Pattern.compile("^.*name=(.*)\n=owner.*$", Pattern.MULTILINE | Pattern.DOTALL);
This is how it happened, but it's clearly somehow crooked ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question