import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\"log\":\".*?(?P<message>Company\\s*[^\\.]+.)\\s*Company:\\s*\\[(?P<company>[0-9]+)\\]\\.\\s*Rate:\\s*\\[(?P<rate>[0-9\\.B\\\\\\/s]+)\\])";
final String string = "{\"kubernetes\":{\"pod_name\":\"throttler-0\",\"namespace_name\":\"default\",\"pod_id\":\"4f91ad3a-dd6f-4d56-b08a-2225edd67f9b\",\"labels\":{\"CX_AZ\":\"us-east-2a\",\"CX_CLOUDFLARE_SITE_ID\":\"ddb3ffb7b6beb645b97e4bab3fbcfc44\",\"CX_CLUSTER_DNS\":\"usprod1.us-east-2.k8s-prod.coralogix.us\",\"CX_DEPLOYABLE\":\"default\",\"CX_DOMAIN\":\"coralogix.us\",\"CX_ENV_CLASS\":\"usprod1\",\"CX_ENV_ID\":\"usprod1\",\"CX_ENV_TYPE\":\"prod\",\"CX_ORG\":\"coralogix\",\"CX_PRODUCT\":\"logs-data\",\"CX_REGION\":\"us-east-2\",\"CX_REPOSITORY\":\"throttler\",\"CX_SERVICE_NAME\":\"throttler\",\"CX_SERVICE_TYPE\":\"application\",\"app_kubernetes_io_instance\":\"throttler\",\"app_kubernetes_io_managed-by\":\"Helm\",\"app_kubernetes_io_name\":\"throttler\",\"controller-revision-hash\":\"throttler-8b99fb774\",\"pipelines.coralogix.net\\/managed-by\":\"eng-pipelines\",\"pipelines.coralogix.net\\/pipelines-version\":\"0.3.42\",\"statefulset.kubernetes.io\\/pod-name\":\"throttler-0\"},\"host\":\"ip-10-11-108-110.us-east-2.compute.internal\",\"container_name\":\"throttler\",\"docker_id\":\"90015d1ac334d32e38f69fa603b22b02a03182d00df7264a006f29b1f4fa066a\",\"container_hash\":\"529726762838.dkr.ecr.eu-west-1.amazonaws.com\\/throttler@sha256:1f8859d7e841f7f22d24bc882f6677199a747c0b40ee2f2ccb1cc4e10f46d4d3\",\"container_image\":\"529726762838.dkr.ecr.eu-west-1.amazonaws.com\\/throttler:392e6bdc\"},\"stream\":\"stdout\",\"log\":\"[INFO] 2022-11-03T07:03:02,464 com.coralogix.throttler.model.Violation throttler-0af20f87-e6c8-4b51-8993-9e726245860a-StreamThread-1 Company transitioned into violated state. Company: [2010806]. Rate: [1298828.7833333334B\\/s].\",\"time\":\"2022-11-03T07:03:02.464514886Z\"}";
final String subst = "\"message\":\"$message\",\"company\":\"$company\",\"rate\":\"$rate\",${1}";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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