Regular Expression to identify Cobol EVALUATE block with JAVA - java

I want a regular expression to extract text between EVALUATE and END-EVALAUTE or . which ever comes first.
Presently i am using regular expression:
EVALUATE\\s*(((?!EVALUATE|(END-EVALUATE|\\.)).)+)\\s*(END-EVALUATE|\\.)
But my problem is i do not want to consider . if it comes within double quotes.
Please suggest any better regular expression or correct the one i have mentioned above.
Thanks in advance.

You could try this:
EVALUATE("[^"]*"|((?!EVALUATE|END-EVALUATE)[^."])+)*(END-EVALUATE|\.)
A Java demo:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws Exception {
String src =
" EVALUATE WS-ADDITIONAL-FILE-WORK \n" +
" WHEN \"ACCNT\" \n" +
" IF LINK-TRIG-FILE-NAME NOT = \"ACTMSTR \" \n" +
" PERFORM 04510-GET-ACCOUNT-MASTER \n" +
" ELSE \n" +
" MOVE \"0106H\" TO WS-ERROR-CODE \n" +
" PERFORM 09000-PROCESS-ABORT-ERROR \n" +
" END-IF \n" +
" WHEN \"ADDRM\" \n" +
" IF LINK-TRIG-FILE-NAME NOT = \"ADDRMSTR \" \n" +
" IF PROGRAM-HBMS-RELEASE (1:3) > \"5.0\" \n" +
" PERFORM 04520-GET-ADDRESS-MASTER \n" +
" END-IF \n" +
" ELSE \n" +
" MOVE \"0106H\" TO WS-ERROR-CODE \n" +
" PERFORM 09000-PROCESS-ABORT-ERROR \n" +
" END-IF \n" +
" WHEN OTHER \n" +
" MOVE \"0106F\" TO WS-ERROR-CODE \n" +
" PERFORM 09000-PROCESS-ABORT-ERROR \n" +
" END-EVALUATE. ";
Matcher m = Pattern.compile("EVALUATE(\"[^\"]*\"|((?!EVALUATE|END-EVALUATE)[^.\"])+)*(END-EVALUATE|\\.)").matcher(src);
while(m.find()) {
System.out.println(m.group());
}
}
}
which prints:
EVALUATE WS-ADDITIONAL-FILE-WORK
WHEN "ACCNT"
IF LINK-TRIG-FILE-NAME NOT = "ACTMSTR "
PERFORM 04510-GET-ACCOUNT-MASTER
ELSE
MOVE "0106H" TO WS-ERROR-CODE
PERFORM 09000-PROCESS-ABORT-ERROR
END-IF
WHEN "ADDRM"
IF LINK-TRIG-FILE-NAME NOT = "ADDRMSTR "
IF PROGRAM-HBMS-RELEASE (1:3) > "5.0"
PERFORM 04520-GET-ADDRESS-MASTER
END-IF
ELSE
MOVE "0106H" TO WS-ERROR-CODE
PERFORM 09000-PROCESS-ABORT-ERROR
END-IF
WHEN OTHER
MOVE "0106F" TO WS-ERROR-CODE
PERFORM 09000-PROCESS-ABORT-ERROR
END-EVALUATE

Just thought I'd point out that the regex given by Bart will match a basic, single-level EVALUATE block, however it will NOT cope with nested EVALUATEs.
For instance, try the regex on the following example:
EVALAUTE TRUE
WHEN FILE-ERROR
EVALUATE ERROR-CODE
WHEN FILE-NOT-FOUND
DISPLAY "File Not Found!"
WHEN ACCESS-DENIED
DISPLAY "Access Denied!"
END-EVALUATE
WHEN OTHER
DISPLAY "Success!"
END-EVALUATE
Another approach would be to read through the Cobol source line-by-line and for each EVALUATE you find on a line (that's not inside quotes), increment an evaluate 'level'. That way you can keep track of where you are in the nested levels.
Also, the OP said he was looking for a way to get the text "between" the EVALUATE and END-EVALUATE, which seems to imply that they should not be included. Maybe I misinterpreted that one, but if that is the requirement, then the regex is including the keywords incorrectly.

Related

How to print the tokens like with "-tokens" in the terminal?

In the terminal (or console), I can just do grun Exp eval -tokens, but how do I do that from Java? I found that, that do like -tree:
ParseTree tree = parser.eval();
System.out.println(tree.toStringTree(parser));
But I can't find anything similar for -tokens.
Edit 1: I found something with lexer.reset():
lexer.reset();
for (Token token: lexer.getAllTokens()) {
System.out.println(token);
}
But the token are only numbers like <4>, I would like to get the real name from it.
Edit 2: I got it:
Vocabulary vocabulary = lexer.getVocabulary();
lexer.reset();
for (Token token: lexer.getAllTokens()) {
System.out.println(token.getLine() + ":" + token.getCharPositionInLine() + " '" + token.getText() + "' " + vocabulary.getSymbolicName(token.getType()));
}
This isn't the exact same thing but the main infos still!
The answer is:
Vocabulary vocabulary = lexer.getVocabulary();
lexer.reset();
for (Token token: lexer.getAllTokens()) {
System.out.println(token.getLine() + ":" + token.getCharPositionInLine() + " '" + token.getText() + "' " + vocabulary.getSymbolicName(token.getType()));
}

How to remove specific line from multi-line string

I have below java string as command output
String output = "NIC Value\n"
+ "------ -----\n"
+ "vmn0 on \n"
+ "vmn1 on \n"
+ "vmn2 on \n"
+ "vmn3 on \n"
+ "vmn4 on";
I want to remove second line with dash from above string. How can I do it?
I tried it using contains method but it is generating blank line after removing second line.
if(output!=null && output.contains("-"))
output = output.replace("-","");
This is complete answer you are looking for:
String output = "NIC Value\n"
+ "------ -----\n"
+ "vmn0 on \n"
+ "vmn1 on \n"
+ "vmn2 on \n"
+ "vmn3 on \n"
+ "vmn4 on";
String str = Stream.of(output.split("\n"))
.filter(s -> !s.contains("--"))
.collect(Collectors.joining("\n"));
You can use this to remove that line and use the result,
String result = output.replace("------ -----\n", "");
It will replace that line with an empty String

cannot find symbol method append (String)

I don't get it why it says it cannot find symbol append.
do i need to use Stringbuffer? i got this code on a tutorial for receipts from youtube, and the uploader disabled comments so I can't ask him directly. please help me. Im still an amateur at java.
Tell me if I need to post my whole code or what code would you want to see to see errors. thanks in adv.
Calendar timer = Calendar.getInstance();
timer.getTime();
SimpleDateFormat tTime = new SimpleDateFormat("HH:mm:ss");
tTime.format(timer.getTime());
SimpleDateFormat Tdate = new SimpleDateFormat("dd-MMM-yyyy");
Tdate.format(timer.getTime());
jtxtReceipt.append("\ Water Station Receipt:\n" +
"Reference:\t\t\t" + refs +
"\n=========================================\n" +
"Mineral:\t\t\t" + jtxtMineral.getText() + "\n\n" +
"Purified:\t\t\t" + jtxtPurified.getText() + "\n\n" +
"Travel:\t\t\t" + jtxtTravel.getText() + "\n\n" +
"VAT:\t\t\t" + jtxtVat.getText() + "\n"+
"\n========================================\n" + "\n" +
"Tax:\t\t\t" + jtxtTax2.getText() + "\n" +
"Subtotal:\t\t\t" + jtxtSubTotal.getText() + "\n" +
"Total:\t\t\t" + jtxtTotal.getText() + "\n" +
"===========================================" +
"\nDate:" + Tdate.format(timer.getTime()) +
"\ntTime:" + tTime.format(timer.getTime()) +
"\n\t\tThank you ");
The append method doesn't exist in the String class. You can either user a StringBuilder to do the job, or if it's a light concatenation, just use the + operator
The append method doesn't work on TextField Palette. So, if You're on TextField Palette, replacing that with TextArea Palette should solve the problem.

How can I add a newline character to a String in Java?

In a Java application, I am creating a String like below (by concatenation):
String notaCorrente = dataOdierna + " - " + testoNotaCorrente;
My problem is that I want to add also something like an HTML newline character at the end of this String (that will be shown into an HTML page).
How can I implement it?
The newline character in Java is "\n" which will look like this:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "\n";
However, this will not display as you expect on your HTML page. You can try adding an html break tag, or add the
(Line Feed) and 
 (Carriage Return) HTML entities:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
or
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "
&#10";
For a newline that will result in a line break in HTML, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
For a newline that will result in a line break in your text editor, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + System.lineSeparator();
And for both, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>" + System.lineSeparator();
Why not \n?
\n is specific to certain operating systems, while others use \r\n. System.lineSeparator() will get you the one that is relevant to the system where you are executing your application. See the documentation for more info on this function, and Wikipedia for more info on newlines in general.
Simply, need to add <br/> (break line tag of HTML).
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br/>";
so, while you are going to display this content, <br/> tag will rendered on HTML page in form of new line.

How can I split the data over multiple lines in a JTextArea?

How can I put each line of the host in the message body? I work with a JTextArea.
String host = InetAddress.getLocalHost().getHostName().toString();
texto_recepcion.setText(host + texto_recepcion.getText() + dpRecepcion.getAddress() + " " + mensaje_recibido + "\n");
How it is now:
I resolve my question with append function.
String host = InetAddress.getLocalHost().getHostName().toString();
texto_recepcion.append(host); // ***Use the function append for solve the problem***
texto_recepcion.setText(texto_recepcion.getText() + dpRecepcion.getAddress() + " " + mensaje_recibido + "\n");
Thanks a lot
Why don't you add the newline character "\n" to the beginning of the string?
texto_recepcion.setText("\n" + host + texto_recepcion.getText() + dpRecepcion.getAddress() + " " + mensaje_recibido);

Categories