Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Regex is not the right tool for analyzing the COBOL grammar; however it could be used when splitting the input text into tokens. But even for this much simpler task, Regex alone will not be sufficient. Additional logic will be required.</p> <p>According to <a href="http://www.cs.vu.nl/grammarware/vs-cobol-ii/#gdef%3aalphabetic-user-defined-word" rel="nofollow">VS COBOL II grammar Version 1.0.4</a> an identifier (they call it "alphabetic-user-defined-word") is defined like this:</p> <blockquote> <p>([0-9]+[-]<em>)</em>[0-9]<em>[A-Za-z][A-Za-z0-9]</em>([-]+[A-Za-z0-9]+)*</p> </blockquote> <p>This definition is complicated, because it ensures that an identifier contains at least one letter. For splitting this requirement could be dropped. If you do that you get this simple expression for identifiers:</p> <blockquote> <p>[0-9A-Za-z]+(-[0-9A-Za-z])*</p> </blockquote> <p>In order to preserve the delimiters when splitting, just put the delimiter into a capture group (between "(" and ")"):</p> <pre><code>string input = "MY-TABLE(WS-INDEX)"; string[] parts = Regex.Split(input, "([0-9A-Za-z]+(-[0-9A-Za-z])*)"); </code></pre> <p>The result will be (without the quotes):</p> <blockquote> <p>""<br> "MY-TABLE"<br> "("<br> "WS-INDEX"<br> ")" </p> </blockquote> <hr> <p><strong>NOTE</strong></p> <p>Many language grammars have nested structures that are defined recursively. Moreover they have special rules for comments and string escapes etc. that make parsing very difficult. Regex can parse such constructs (see <a href="http://www.regular-expressions.info/recurse.html" rel="nofollow">Regular Expression Recursion and Matching Balanced Constructs</a>) but the Regex expressions become very complicated and very difficult to understand, because you have to squeeze the whole grammar of the language to be parsed into one single Regex expression. It's as if you tried to write a C# application as one single statement. Use a tool specialized tool like <a href="http://irony.codeplex.com/" rel="nofollow">Irony - .NET Language Implementation Kit</a> or <a href="http://en.wikipedia.org/wiki/Coco/R" rel="nofollow">Coco/R</a> instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload