-2

I am trying to filter some content from a file whose content is accessible to me as String as :

block "block1"
{
ADDSFDJF
SDFSDFSDF
SDFSDFSDF

 // subblock: subblock1 [master]
 include "/path/tofile/subblock1.conf";
+/- subblock subblock1
// subblock: subblock1
subblock "subblock1"
{
 type TYPE;
 file "name.file";
 details  blah blah.
 other {sdhsdf};
 };
};

 file "dddd.file";
 details  blah blah.

 // subblock: subblock2
 include "/path/tofile/subblock2.conf";
+/- subblock subblock2
// subblock: subblock2
subblock "subblock2"
{
 type TYPE;
 file "name.file";
 details  blah blah.
 other {sdhsdf};
 more fields
};
};

Here i want to delete some lines of each sub-block, i.e.: i want to remove these lines:

+/- subblock subblock1
// subblock: subblock1
subblock "subblock1"
{
 type TYPE;
 file "name.file";
 details  blah blah.
 other {sdhsdf};
 };
};

I was trying content.replaceALL(); but i am not able to create proper regex which matches '+/-' for start and proper closing '}' parenthesis for it.

Please help me through this.

[EDIT] : regex as a Java string

2 Answers2

0

You might be able to get along with:

^\s*
subblock
[\s\S]+?
^\s*\};$

See a demo on regex101.com.
Note: you must double escape the backslashes in Java.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • @ApoorvJain: You might then accept and/or upvote the answer (green tick on the left). – Jan Aug 25 '16 at 12:40
0

I was able to achieve this through regex :

 (?m)\\+\\/\\-[\\s\\S]+?//[\\s\\S]+?(^\\};\\s+$)

as java string. or

 \+\/\-[\s\S]+?//[\s\S]+?(^\};\s+$)

as python regex.

Here, Pattern.MULTILINE or (?m) tells Java to accept the anchors ^ and $ to match at the start and end of each line (otherwise they only match at the start/end of the entire string). Ref: https://stackoverflow.com/a/3652392/5580576

See : Demo on regex101.com !!

Community
  • 1
  • 1