I'm parsing log files, and need to do one thing when a regex is found (add ',TRUE' to a variable, .csv, ArrayList), and something else (add an empty slot, "," or ",_" if an empty slot isn't good) when that regex is not found.
There can be 1-~200 entries being parsed from each log file - so the regex could match none-to-several times.
I've tried several options, either get an error on compile (not liking testing a Boolean against a string), no observable action occurs, or when the regex is found I get the result for when regex not found.
I need the output in a variable, .csv or an ArrayList, so if a match is not found, then I need an empty ',' because the output is a pre-defined table, so the columns need to align properly.
Combos I've tried:
if(!noBaseText.equals("No matching base data found"))
if(!noBaseText.equals("No matching base data found") == false)
if(noBaseText.equals("No matching base data found" != true))
if(noBaseText.isEmpty())
if(noBaseText.equals(""))
if(!noBaseText.equals(""))
if(noBaseText.equals("No matching base data found"))
'} else {' and '} else if (...) {' with the combos above, both when a nested if/else (as in code below) or as an 'else' to the 'if (baseMatcher.find())' return identical results.
Code Snippet:
Pattern noBase = Pattern.compile("(?<noBase>No matching base data found)");
try(corrReader)
{
while ((corrLine = corrReader.readLine())!=null)
{
corrText = corrLine.trim();
Matcher baseMatcher = noBase.matcher(corrText);
if (baseMatcher.find())
{
String noBaseText = baseMatcher.group("noBase");
if(!noBaseText.equals("No matching base data found"))
{
corrOutput += ",";
} else if(noBaseText.equals("No matching base data found")) {
corrOutput += ",TRUE";
} //end else
} //end if(baseMatcher)
} //end while(corrLine)
} //end try(corrReader)
The output I'm getting when there is a match:
AA-123-12345-SP1.SSF,TRUE,
The output I'm getting when there isn't a match:
AA-123-12345-SP2.SSF,100,100,guug04314054.zip,
The output I need to get when there isn't a match:
AA-123-12345-SP2.SSF,,100,100,guug04314054.zip,
When the regex matches, and ',TRUE' is returned, there will be no following entries; when the regex does not match and a ',' is returned, there will be additional entries.
You can use the ternary operator to accomplish this:
boolean found = false;
while ((corrLine = corrReader.readLine())!=null)
{
...
String noBaseText = baseMatcher.group("noBase");
found |= noBaseText.equals("No matching base data found");
...
}
corrOutput += found ? "TRUE," : ",";
If the found
evaluates to true, you'll get "TRUE,"
otherwise ","
.
I am not sure if I got this right, but.. if you want to check if pattern is empty, do the following:
if(noBase.pattern().isEmpty()) {
// do your stuff
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.