Reputation: 73
I have the following string
aaaa#include(soap1.xml)bbbb #include(soap2.xml)cccc #include(soap2.xml)
I want to find all occurrences of #include([anyfilename])
where [anyfilename]
varies.
I have the regex (?<=#include\()(.*?)(?=\)*\))
which matches [anyfilename]
but then performing a replace using this leaves behind there #include()
Can someone suggest show me how to find/replace the entire #include([anyfilename])
?
Upvotes: 2
Views: 118
Reputation: 627468
You may use the following regex:
#include\(([^)]*)\)
See the regex demo
I replaced lookarounds (that are zero-width assertions and do not consume text, do not return it in the match value) with consuming equivalents.
The regex breakdown:
#include\(
- match a sequence of literal symbols #include(
([^)]*)
- Group 1 (we'll refer to the value inside the group with matcher.group(1)
) matching zero or more characters other than )
\)
- match a literal )
The same pattern can be used to retrieve the filenames, and remove whole #include()
s from the input.
String str = "aaaa#include(soap1.xml)bbbb#include(soap2.xml)cccc";
String p = "#include\\(([^)]*)\\)";
Pattern ptrn = Pattern.compile(p);
Matcher matcher = ptrn.matcher(str);
List<String> arr = new ArrayList<String>();
while (matcher.find()) {
arr.add(matcher.group(1)); // Get the Group 1 value, file name
}
System.out.println(arr); // => [soap1.xml, soap2.xml]
System.out.println(str.replaceAll(p, "")); // => aaaabbbbcccc
Upvotes: 1