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()));
}
Related
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
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.
I have an XML output like this (<xml> element or xlink:href attribute are just fiction and you cannot rely on them to create regex pattern.)
<xml>http://localhost:8080/def/abc/xyx</xml>
<element xlink:href="http://localhostABCDEF/def/ABC/XYZ">Some Text</element>
...
What I want to do is using Java regex to replace the domain pattern (I don't know about existing domains):
"http(s)?://.*/def/.*
with an input domain (e.g: http://google.com/def) and the result will be:
<xml>http://google.com/def/abc/xyx</xml>
<element xlink:href="http://google.com.com/def/ABC/XYZ">Some Text</element>
...
How can I do it? I think Regex in Java can do or String.replaceAll (but this one seems not possible).
Regex: http[s]?:\/{2}.+\/def Substitution: http://google.com/def
Details:
? Matches between zero and one times
[] Match a single character present in the list
. Matches any character
+ Matches between one and unlimited times
Java code:
String domain = "http://google.com/def";
String html = "<xml>http://localhost:8080/def/abc/xyx</xml>\r\n<element xlink:href=\"http://localhostABCDEF/def/ABC/XYZ\">Some Text</element>";
html = html.replaceAll("http[s]?:\\/{2}.+\\/def", domain);
System.out.print(html);
Output:
<xml>http://google.com/def/abc/xyx</xml>
<element xlink:href="http://google.com/def/ABC/XYZ">Some Text</element>
Actually, this could be done with Regex and it is simple enough than parsing XML document. Here is the answer:
String text = "<epsg:CommonMetaData>\n"
+ " <epsg:type>geographic 2D</epsg:type>\n"
+ " <epsg:informationSource>EPSG. See 3D CRS for original information source.</epsg:informationSource>\n"
+ " <epsg:revisionDate>2007-08-27</epsg:revisionDate>\n"
+ " <epsg:changes>\n"
+ " <epsg:changeID xlink:href=\"http://www.opengis.net/def/change-request/EPSG/0/2002.151\"/>\n"
+ " <epsg:changeID xlink:href=\"http://www.opengis.net/def/change-request/EPSG/0/2003.370\"/>\n"
+ " <epsg:changeID xlink:href=\"http://www.opengis.net/def/change-request/EPSG/0/2006.810\"/>\n"
+ " <epsg:changeID xlink:href=\"http://www.opengis.net/def/change-request/EPSG/0/2007.079\"/>\n"
+ " </epsg:changes>\n"
+ " <epsg:show>true</epsg:show>\n"
+ " <epsg:isDeprecated>false</epsg:isDeprecated>\n"
+ " </epsg:CommonMetaData>\n"
+ " </gml:metaDataProperty>\n"
+ " <gml:metaDataProperty>\n"
+ " <epsg:CRSMetaData>\n"
+ " <epsg:projectionConversion xlink:href=\"http://www.opengis.net/def/coordinateOperation/EPSG/0/15593\"/>\n"
+ " <epsg:sourceGeographicCRS xlink:href=\"http://www.opengis.net/def/crs/EPSG/0/4979\"/>\n"
+ " </epsg:CRSMetaData>\n"
+ " </gml:metaDataProperty>"
+ "<gml:identifier codeSpace=\"OGP\">http://www.opengis.net/def/area/EPSG/0/1262</gml:identifier>";
String patternString1 = "(http(s)?://.*/def/.*)";
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(text);
String prefixDomain = "http://localhost:8080/def";
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String url = prefixDomain + matcher.group(1).split("def")[1];
matcher.appendReplacement(sb, url);
System.out.println(url);
}
matcher.appendTail(sb);
System.out.println(sb.toString());
which returns output https://www.diffchecker.com/CyJ8fY8p
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);
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.