import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^\\s+(?:re)?start.*?(haproxy_check).*?;;";
final String string = "case \"$1\" in\n"
+ " start)\n"
+ " echo -n \"Starting haproxy \"\n"
+ " ## Start daemon with startproc(8). If this fails\n"
+ " ## the return value is set appropriately by startproc.\n"
+ " haproxy_check\n"
+ " /sbin/startproc $HAPROXY_BIN -D -f $HAPROXY_CONF -p $HAPROXY_PID\n"
+ " # Remember status and be verbose\n"
+ " rc_status -v\n"
+ " ;;\n"
+ " stop)\n"
+ " echo -n \"Shutting down haproxy \"\n"
+ " ## Stop daemon with killproc(8) and if this fails\n"
+ " ## killproc sets the return value according to LSB.\n\n"
+ " /sbin/killproc -TERM $HAPROXY_BIN\n\n"
+ " # this is additional forcing kill command to ensure that all processes are stopped.\n"
+ " /usr/bin/killall -9 $HAPROXY_BIN || true\n"
+ " # Remember status and be verbose\n"
+ " rc_status -v\n"
+ " ;;\n"
+ " try-restart|condrestart)\n"
+ " ## Do a restart only if the service was active before.\n"
+ " ## Note: try-restart is now part of LSB (as of 1.9).\n"
+ " ## RH has a similar command named condrestart.\n"
+ " if test \"$1\" = \"condrestart\"; then\n"
+ " echo \"${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}\"\n"
+ " fi\n"
+ " $0 status\n"
+ " if test $? = 0; then\n"
+ " # we us reload here for a graceful restart during update\n"
+ " $0 reload\n"
+ " else\n"
+ " rc_reset # Not running is not a failure.\n"
+ " fi\n"
+ " # Remember status and be quiet\n"
+ " rc_status\n"
+ " ;;\n"
+ " restart)\n"
+ " ## Stop the service and regardless of whether it was\n"
+ " ## running or not, start it again.\n"
+ " $0 stop\n"
+ " $0 start\n"
+ " haproxy_check\n"
+ " # Remember status and be quiet\n"
+ " rc_status\n"
+ " ;;\n"
+ " check)\n"
+ " ## Stop the service and regardless of whether it was\n"
+ " ## running or not, start it again.\n"
+ " echo -n \"Checking config of haproxy \"\n"
+ " haproxy_check\n"
+ " rc_status -v\n"
+ " ;;\n"
+ " reload|force-reload)\n"
+ " ## Like force-reload, but if daemon does not support\n"
+ " ## signaling, do nothing (!)\n\n"
+ " haproxy_check\n"
+ " # If it supports signaling:\n"
+ " echo -n \"Reload service haproxy \"\n"
+ " $HAPROXY_BIN -p $HAPROXY_PID -D -f $HAPROXY_CONF -sf $(cat $HAPROXY_PID)\n"
+ " rc_status -v\n"
+ " ;;\n"
+ " status)\n"
+ " echo -n \"Checking for service haproxy \"\n"
+ " ## Check status with checkproc(8), if process is running\n"
+ " ## checkproc will return with exit status 0.\n\n"
+ " # Return value is slightly different for the status command:\n"
+ " # 0 - service up and running\n"
+ " # 1 - service dead, but /var/run/ pid file exists\n"
+ " # 2 - service dead, but /var/lock/ lock file exists\n"
+ " # 3 - service not running (unused)\n"
+ " # 4 - service status unknown :-(\n"
+ " # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)\n\n"
+ " # NOTE: checkproc returns LSB compliant status values.\n"
+ " /sbin/checkproc $HAPROXY_BIN\n"
+ " # NOTE: rc_status knows that we called this init script with\n"
+ " # \"status\" option and adapts its messages accordingly.\n"
+ " rc_status -v\n"
+ " ;;\n"
+ " probe)\n"
+ " ## Optional: Probe for the necessity of a reload, print out the\n"
+ " ## argument to this init script which is required for a reload.\n"
+ " ## Note: probe is not (yet) part of LSB (as of 1.9)\n\n"
+ " test $HAPROXY_CONF -nt $HAPROXY_PID && echo reload\n"
+ " ;;\n"
+ " *)\n"
+ " echo \"Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}\"\n"
+ " exit 1\n"
+ " ;;\n"
+ "esac";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html