Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Looking over the LPeg docs I do not see a way to have alternatives(OR)

http://www.inf.puc-rio.br/~roberto/lpeg

e.g., a | b will match a or b. Am I missing something?

share|improve this question

closed as off topic by topskip, Paulo Cereda, egreg, lockstep, percusse Mar 18 '12 at 20:38

Questions on TeX - LaTeX Stack Exchange are expected to relate to TeX, LaTeX or related typesetting systems within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
This question belongs on Stack Overflow, it has no real relationship with TeX. –  topskip Mar 18 '12 at 20:18

1 Answer 1

up vote 3 down vote accepted

From the documentation you linked to, we can read that patt1 + patt2 matches patt1 or patt2, in this order.

Something like the following should work (but I'm no LuaTeX expert).

\def\test#1{%
  \directlua{
    require('lpeg');
    tex.print(lpeg.match(lpeg.S('0123456789')+lpeg.P(3),'#1'))
}}
\test{123}
\test{abc}

Probably '#1' should be replaced by the appropriate LuaTeX primitive to escape #1 properly.

share|improve this answer
    
I don't follow your code. I do not want an ordered match but an unordered match. I think the '-' symbol does the job but not sure(the description given on the site is a little strange but may be equivalent to OR). –  Uiy Mar 18 '12 at 1:33
1  
There is no such thing as an unordered OR. Here, "ordered" denotes the fact that first patt1 is tested, then if the first one failed, patt2 is tried. This is identical to the | of regular expressions in Perl 5, and most other regular expression engines, but not to POSIX-compliant engines. –  Bruno Le Floch Mar 18 '12 at 2:17
    
huh? | is OR in all regular expressions I know of. (a | b | c) will match any one of a b or c. From wiki: "A vertical bar separates alternatives. For example, gray|grey can match "gray" or "grey"." Note the order does not matter. i.e., unordered! –  Uiy Mar 18 '12 at 2:48
1  
@Uiy The order does not matter if the only question is "is there a match?" However, the order is important when your goal is to extract submatches. For instance, most regex engines searching for (ab|abc) in abcd will capture ab, but exchanging the two branches will give abc. One notable exception is Perl 6: I believe it returns the longest match, but the details are more complicated. –  Bruno Le Floch Mar 18 '12 at 6:04
1  
@Uiy: I beg to differ. As is documented, lpeg.P('ab')+lpeg.P('abc') will first try to match ab, and if this fails, try to match abc (of course, with those particular patterns, the second one will never match). On the other hand, lpeg.P('ab')-lpeg.P('abc') will match ab, unless it is followed by c, in which case it will fail to match. What you describe, concatenation, is achieved with *. Namely, lpeg.P('ab')*lpeg.P('abc') is equivalent to lpeg.P('ababc'). –  Bruno Le Floch Mar 18 '12 at 6:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.