Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

need help with parsing G code

Last post 05-12-2008, 2:51 AM by kevin. 5 replies.
Sort Posts: Previous Next
  •  05-08-2008, 12:41 PM 42012

    need help with parsing G code

    I need a regex that matches a string containing only the letters of the english alphabet, upper and lower case, and digits. I'm trying to parse a line of g-code and get the parameters.  

    e.g the g code will be in this format: "Gxx Xxx Yxx Ixx Jxx Mxx",  For this input "G01 X34 Y56 M30", these values should match G:01, X:34, Y56, I:null, J:null, M:30. and for this input "G02 X34 Y56 I0 J78", these values should be: G:02, X:34, Y56, I:0, J:78, M:null.

    Is that very difficult? I am doing a course with VB.NET, looking for help!Smile

  •  05-08-2008, 1:21 PM 42025 in reply to 42012

    Re: need help with parsing G code

    You are saying you want to input one string and match something else. What role does a regex play in this?

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-08-2008, 1:40 PM 42029 in reply to 42012

    Re: need help with parsing G code

    Assuming the following:

    - You will never have a letter you want to match that is not GXYIJM

    - The order is not mandatory

    You can use this regex: (?:(?<Letter>[GXYIJM])(?<Value>\d+)\s*)*

    You will get two named capturing groups as a result: The first one, called 'Letter' will hold the Letter.  The matching Value group holds the value.  Any letter that is not included in the matching groups is not present in the string, and therefore null.

     

  •  05-11-2008, 8:05 AM 42110 in reply to 42029

    Re: need help with parsing G code

    Thank you very much!

    It seems that I didn't explain clearly enough. I try your regex and it works, but it can match only one result.

    What I want is to get all the decimal values.

    e.g. if the input string is "G01 X23 Y45 I0 J0 M67", then the match results would be follow:

    G=01

    X-23

    Y=45

    I=0

    J=0

    M=67

    And your regex will get M=67.

    So, any good idea?

    Thanks a lot!

  •  05-11-2008, 8:15 PM 42115 in reply to 42110

    Re: need help with parsing G code

    Kevin,

    Try:

    (
      (
        G(?<Gdata>\d+)
        |
        X(?<Xdata>\d+)
        |
        Y(?<Ydata>\d{2})
        |
        I(?<Idata>\d{1,2})
        |
        J(?<Jdata>\d+)
        |
        M(?<Mdata>\d+)
      )
      \s*
    )+

    I've spread this out over multiple lines so that you can see how it operates (you said you were doing a course so I'm assuming that you want to understand the pattern, nit just get an answer). Obviously, you should make this a single line or use the 'ignore whitespace' .NET option.

    There are several variants in the lines because I'm not sure what you are really wanting. In your original question you shoe each letter followed by 2 X's and indicate that these are numbers. This would lead to the "\d{2}" style sub-pattern. However, you also give the example of "j0" which only has a single digit which means that "\d{1,2}" might be more approrpiate. Given that confusion, you might also have meant that there could be any number of digits after each letter, in which case the "\d+" pattern is more appropriate.

    If you look at the returned named groups, you will find the corresponding number strings or 'null' as desired.

    Susan 

    PS Lyndar's answer is also 'correct' because you are using the .NET regex engine - if you looked at the captures within the named matches you would see all of the results you asked for.

     

  •  05-12-2008, 2:51 AM 42120 in reply to 42115

    Re: need help with parsing G code

    I am realy appreciate for your help! Thank you very much for your explanation, now I understand it more well. THX!
View as RSS news feed in XML