#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Regex and Parenthesis


    hello guys,
    sorry for my english. I have a "Regex" problem.

    This is a simple source (Iso Language for CNC):
    Code:
    ...
    G0 G40 G80 G90 G98
    G0 G91 G28 Z0
    T11 (PUNTA   6.20 M.D.INT.DIN 1897)
    M6
    G0 G90 G55 X-390 Y-15 M3 S4897
    ...
    And I need to select characters from "T\d+" to "M6".
    My pattern is:
    Code:
    T\d+(.(?!T\d+))*?M6
    The result is:
    Code:
    T11 (PUNTA   6.20 M.D.INT.DIN 1897) M6
    Good!
    The pattern works well, but I have a problem when "T\d+" Or "M6" are brackets.

    Pattern sample:
    Code:
    ...
    G0 G40 G80 G90 G98
    G0 G91 G28 Z0
    T11 (MASCHIO M6)
    M6
    G0 G90 G55 X-390 Y-15 M3 S4897
    ...
    The result is:
    Code:
    T11 (MASCHIO M6


    In ISO code for CNC, the word "M6" is a command for tool change, but is valid only if it's not in brackets.
    The result would ne that I would like this:
    Code:
    T11 (MASCHIO M6) M6
    How can I not consider the words that are in parentheses?
    If everything is clear, I hope you can help me.

    Thank you very much.

    Bye
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,743
    Rep Power
    9397
    Is the Tn always the first thing on the line? Most importantly is the M6 always the first thing on the next line?
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    Hi,

    Regular expressions are the completely wrong tool for this. Look for a CNC parser and then use it to process the actual code structure (instead of character sequences).

    Regexes are not the solution for everything. Even if you can misuse them for a lot of things, the result will just be cumbersome, ugly, inflexible and error-prone.

IMN logo majestic logo threadwatch logo seochat tools logo