Siehe die folgende XML-Datei. Die $ BASE_DIR, $ ENV_PREFIX usw. sind Umgebungsvariablen in einem Linux-Betriebssystem. Ich möchte die Variablen des Formats $ gefolgt von einem beliebigen Buchstaben, dann einem Buchstaben oder einer Zahl bis zum < Symbol finden, wo das Tag geschlossen ist, und diese in einer Liste speichern. Bitte beachten Sie diejenigen, bei denen $ {envPrefix} später durch eine API von Drittanbietern ersetzt wird und in dieser Liste ignoriert werden sollte.Behandeln der Anforderung mit regulärem Ausdruck
<?xml version="1.0" encoding="UTF-8"?>
<myConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://myconfig.xsd">
<configVariable name="baseDir" type="java.lang.String">
<value>$BASE_DIR</value>
</configVariable>
<configVariable name="envPrefix" type="java.lang.String">
<value>$ENV_PREFIX</value>
</configVariable>
<configVariable name="logPath" type="java.lang.String">
<value>${baseDir}/${envPrefix}</value>
</configVariable>
<configVariable name="appName">
<value>Publish</value>
</configVariable>
<configVariable name="CUSTOM_LOG">
<value>$CUSTOM_LOG</value>
</configVariable>
<configVariable name="PUBLISH_LOG">
<value>$PUBLISH_LOG</value>
</configVariable>
<logging>
<destination type="file" maxFileBackup="16" maxFileSize="10MB"
filePath="${PUBLISH_LOG}/${appName}.log" smLogName="publish" />
<priority level="info" smLogName="publish" />
</logging>
.......
</myConfig>
String fileContent="the_file_provided_above";
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile(Pattern.quote("????What goes here">)).matcher(fileContent);
while (m.find()) {
allMatches.add(m.group());
}
Ich versuche herauszufinden, den regulären Ausdruck, der hier gehen muss, ich bin nicht sicher, was nach $ geht, um damit umzugehen. Ich habe versucht, etwas wie unten nur um es funktioniert zu bekommen ... Matcher m = Pattern.compile (Pattern.quote ("$ ENV_PREFIX")). Matcher (fileContent); \t \t während (m.find()) { \t \t \t allMatches.add (m.gruppe()); \t \t} –