nikink Posted May 10, 2021 Share Posted May 10, 2021 Hi all, I'm struggling to get some regex patterns right and am hoping someone better at them than me can help. I have a string that looks like: N: nikink-vm2, nikink-vm, xstf1111a9lt986 M: ABCDEFABCDEG, 12acac12acac O: 123 D: Stuff and details and things ON 1234 The O: is order number, the N: is name(s), the M: is mac address(es), the D : is old information (all alphanum plus colon, dash, and comma). The O/N/M/D fields are all optional - they may or may not exist, and they might be in any order. (My script is part of a move to ensure all fields are present even if empty) So if they *do* exist I want to parse out the values - for example GetOrderNumer returns "123", GetNames returns "nikink-vm2, nikink-vm, xstf1111a9lt986" and so on. My regex patters so far are: $NamesPattern = "(?:N:\s*([A-Za-z0-9-,\s]*)\s)" $MacssPattern = "(?:M:\s*([A-Za-z0-9-,\s]*)\s?[^O:|N:|M:|D:])" $OrderPattern = "(?:O:\s*([0-9]*)\s?)" $DetailsPattern = "(?:D:\s*([A-Za-z0-9-,\s]*)\s?[^O:|N:|M:])" And they are very close to working correctly! But for example, if I move the N: filed from the front to the end of that string, the pattern returns only "nikink-vm2, nikink-vm," (completely missing the third entry). If I move the Mac field to the end it returns "ABCDEFABCDEG, 12acac12aca" (cutting off the final 'c'). When I start refining the regex my lack of actual understanding shows. For example using: (?:(N:\s*([A-Za-z0-9-]*(,?(\s)*)?)*\s*[^M:])) to capture names gives me a leading N: (which I don't want if it can be avoided). The [^M:] at the end is just my attempt to stop the pattern from returning the following 'M' and because the fields could be in any order that needs to account for O and D as well and I'm having a hell of a time getting that to work as well. In all cases I'm testing this with the stringregexgui.au3 from the StringRegExp help file. Any help would be greatly appreciated. Link to comment Share on other sites More sharing options...
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!Register a new account
Already have an account? Sign in here.Sign In Now