FrancescoDiMuro Posted May 19, 2021 Share Posted May 19, 2021 Good morning yall! I wonder how could I capture some values which are part of a string, where a particular prefix appears multiple times, with different values after it. This is the input string: User: SomeUser Login-name: SomeLoginName NTSecurity: YES Domain: SomeDomain Timeout: 00:00:00 Member: MemberOfFirstGroup Member: MemberOfSecondGroup Member: MemberOfThirdGroup This is the output it should produce: SomeUser SomeLoginName YES MemberOfFirstGroup MemberOfSecondGroup MemberOfThirdGroup So, practically, the Domain and Timeout parameters are ignored by the pattern, which look like this (without the "Member:" captouring group): User:\s([^\r\n]+)\s* Login\-name:\s([^\r\n]+)\s* .*?\s* NTSecurity:\s([^\r\n]+)\s* I'm using this pattern in VBScript, so I had to change it a bit from AutoIt regex "pattern' style". Could you please enlight me on how to do that? Thanks a lot. Francesco Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Nine Posted May 19, 2021 Share Posted May 19, 2021 This ? #include <Constants.au3> $sText = _ "User: SomeUser" & @CRLF & _ "Login-name: SomeLoginName" & @CRLF & _ "NTSecurity: YES" & @CRLF & _ "Domain: SomeDomain" & @CRLF & _ "Timeout: 00:00:00" & @CRLF & _ "Member: MemberOfFirstGroup" & @CRLF & _ "Member: MemberOfSecondGroup" & @CRLF & _ "Member: MemberOfThirdGroup" MsgBox ($MB_SYSTEMMODAL, "", StringRegExpReplace($sText, "User:\s|Login-name:\s|NTSecurity:\s|Domain:\s.*\v+|Timeout.*\v+|Member:\s", "")) FrancescoDiMuro 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 19, 2021 Author Share Posted May 19, 2021 @Nine Always a pleasure to see you here around I forgot to mention that these information are part of a bigger string, in which there's a lot of data, so, the best solution would be to capture those values instead of removing what is not needed. I like the approach by the way, thanks Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Nine Posted May 19, 2021 Share Posted May 19, 2021 Do you prefer this one ? #include <Constants.au3> $sText = _ "User: SomeUser" & @CRLF & _ "Login-name: SomeLoginName" & @CRLF & _ "NTSecurity: YES" & @CRLF & _ "Domain: SomeDomain" & @CRLF & _ "Timeout: 00:00:00" & @CRLF & _ "Anything: Before" & @CRLF & _ "Member: MemberOfFirstGroup" & @CRLF & _ "Member: MemberOfSecondGroup" & @CRLF & _ "Member: MemberOfThirdGroup" & @CRLF & _ "Anything: After" MsgBox ($MB_SYSTEMMODAL, "", StringRegExpReplace($sText, "(?|User|Login-name|NTSecurity|Member):\s(.*)|.+\v*", "$1")) FrancescoDiMuro 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 19, 2021 Author Share Posted May 19, 2021 @Nine Definitely yes! Thank you so much for you help, really appreciated I'll flag this thread as solved, but if anyone else (just for fun) want to try another flavours, they are highly appreciated as well. Have a good day! Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 19, 2021 Author Share Posted May 19, 2021 @Nine Bad news... Seems that branch reset group is not supported from VBScript, so I currently can't use your solution, even if I already adapted the code to work with that. Is it possible to have the same result without branch reset groups? Thanks for your help Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Nine Posted May 19, 2021 Share Posted May 19, 2021 Try this simpler pattern : MsgBox ($MB_SYSTEMMODAL, "", StringRegExpReplace($sText, "(User|Login-name|NTSecurity|Member):\s(.*)|.+\v*", "$2")) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 19, 2021 Author Share Posted May 19, 2021 (edited) @Nine The above pattern returns a string without \n, so data is formatted like this: SomeUser1 SomeLoginName1 YES MemberOfFirstGroup1 MemberOfSecondGroup1 MemberOfThirdGroup1 SomeUser2 SomeLoginName2 YES MemberOfFirstGroup2 instead of: SomeUser1 SomeLoginName1 YES MemberOfFirstGroup1 MemberOfSecondGroup1 MemberOfThirdGroup1 SomeUser2 SomeLoginName2 YES MemberOfFirstGroup2 and doing so, I have no discriminating string to know where a new "group" of information starts (and so, neither where a group of information ends). Fun fact is if I try both patterns with RegEx101.com, I have a different result from VBScript one. Ideally, and based on the result I had from RegEx101.com, every group of information is divided by two \n, and so, I can split those "chunks" in groups which handle one user information, which I then use throughout the script. Thanks again for you help. Edited May 19, 2021 by FrancescoDiMuro Added more information Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Nine Posted May 19, 2021 Share Posted May 19, 2021 If there is only @LF (\n) at the end of each line and there is \n between groups, this should work : #include <Constants.au3> $sText = _ "User: SomeUser" & @LF & _ "Login-name: SomeLoginName" & @LF & _ "NTSecurity: YES" & @LF & _ "Domain: SomeDomain" & @LF & _ "Timeout: 00:00:00" & @LF & _ "Anything: Before" & @LF & _ "Member: MemberOfFirstGroup" & @LF & _ "Member: MemberOfSecondGroup" & @LF & _ "Member: MemberOfThirdGroup" & @LF & _ "Anything: After" & @LF & @LF & _ "User: SomeUser2" & @LF & _ "Login-name: SomeLoginName2" & @LF & _ "NTSecurity: YES" & @LF & _ "Domain: SomeDomain" & @LF & _ "Timeout: 00:00:00" & @LF & _ "Anything: Before" & @LF & _ "Member: MemberOfFirstGroup2" & @LF & _ "Member: MemberOfSecondGroup2" & @LF & _ "Member: MemberOfThirdGroup2" & @LF & _ "Anything: After" MsgBox ($MB_SYSTEMMODAL, "", StringRegExpReplace($sText, "(User|Login-name|NTSecurity|Member):\s(.*)|.+\n?", "$2")) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 19, 2021 Author Share Posted May 19, 2021 @Nine Spoiler As you can see, the various lines are divided by @CR and @LF, but even with the pattern above, the script returns a string with just @CR @LF after each line, without blank lines as they appears in the original file (image below), so I assume that they're removed. Spoiler As I said before, AutoIt and VBScript supports different regex motors, and so, what works on AutoIt may not work on VBScript. Thanks for your kind help Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Deye Posted May 19, 2021 Share Posted May 19, 2021 how about sharing a small reproduction file to read off the data, you are reading from a file right ? Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 19, 2021 Author Share Posted May 19, 2021 @Deye You are definitely right. I am sorry that I didn't post it yet, but I have been busy all day trying to figure out how to structure the script, and since the file contains only sensitive data, it would have taken a lot to replace it with test data. In fact, what I did post is what I have in the source file, but it's "filtered", and @Nine provided valid patterns which work outside VBScript, but not with it, so, it has nothing to do with data itself; rather, it's something that might be about VBScript. I need to make some tests with AutoIt instead of using VBScript and see if the results are the same, but I already know they won't. I'll let you know as soon as possible. Thanks Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Deye Posted May 19, 2021 Share Posted May 19, 2021 No worries, until then, we'll go fishing with 9 to cool him down a bit. FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
Nine Posted May 19, 2021 Share Posted May 19, 2021 49 minutes ago, Deye said: we'll go fishing with 9 to cool him down a bit Dam How do you know fishing is my second life ? FrancescoDiMuro and Deye 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 20, 2021 Author Share Posted May 20, 2021 (edited) @Nine, @Deye Here I am with a sample file and some tests done. The file uploaded has the same structure that has the original one, but with a lot less information which are not needed to be in the sample file. I made tests with this pattern in AutoIt, and blank lines are left there as they were, while doing the same test with VBScript, they are removed from the source file. Here below, you can find both AutoIt script and VBScript to test with the sample file. Let me know what are your results with the scripts, even if I am quite convinced that VBScript won't be able to bring the same result has AutoIt is doing. As always, a big thanks for both of you! P.S.: Happy fishing AutoIt: #include <FileConstants.au3> #include <StringConstants.au3> Test() Func Test() Local $strFileName = @ScriptDir & "\SECURITY.RPT", _ $hdlFile, _ $strFileContent $hdlFile = FileOpen($strFileName, $FO_READ) If Not $hdlFile Then Return ConsoleWrite("FileOpen ERR: " & $hdlFile & @CRLF) $strFileContent = FileRead($hdlFile) If @error Then Return ConsoleWrite("FileRead ERR: " & @error & @CRLF) $strFileContent = StringRegExpReplace($strFileContent, "(?|User|Login-name|NTSecurity|Member):\s(.*)|.+\v*", "$1") If @error Then Return ConsoleWrite("StringRegExpReplace ERR: " & @error & @CRLF) ConsoleWrite($strFileContent & @CRLF) FileClose($hdlFile) EndFunc VBScript: Dim strFileName: strFileName = "SECURITY.rpt" Dim objFSO Dim objTextStreamIn Dim objTextStreamOut Dim strFileContent Dim objRegEx Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextStreamIn = objFSO.OpenTextFile(strFileName, 1, -1) Set objTextStreamOut = objFSO.CreateTextFile(".\Result.txt") strFileContent = objTextStreamIn.ReadAll objTextStreamIn.Close Set objRegEx = CreateObject("VBScript.RegExp") With objRegEx .Pattern = "(User|Login-name|NTSecurity|Member):\s(.*)|.+\n?" .Global = True .IgnoreCase = False .Multiline = True objTextStreamOut.Write(.Replace(strFileContent, "$2")) End With objTextStreamOut.Close Set objRegEx = Nothing Set objTextStreamOut = Nothing Set objTextStreamIn = Nothing Set objFSO = Nothing SECURITY.RPT Edited May 20, 2021 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Deye Posted May 20, 2021 Share Posted May 20, 2021 (edited) Please Try: $sText = FileRead("SECURITY.RPT") $sNames = "User|Login-name|Member|Domain|NTSecurity" $s = StringRegExpReplace($sText, _ ".*(?:" & $sNames & "):\s(.*)|(.+)\R", "\1") ConsoleWrite(StringStripWS($s, 3) & @CRLF) Edited May 20, 2021 by Deye FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 20, 2021 Author Share Posted May 20, 2021 @Deye Thanks for the try, but if you tested the pattern with VBScript, you'll see that it doesn't work. I'd like to kindly remind once again that I'm trying to avoid to use AutoIt for our customer's policies. Thank you Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Deye Posted May 20, 2021 Share Posted May 20, 2021 this seems to work: "(User|Login-name|NTSecurity|Member):\s(.*)|.+\r\n?" FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 20, 2021 Author Share Posted May 20, 2021 @Deye Close, but it leaves blank lines at the top of the string. Did you have the same result? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Deye Posted May 20, 2021 Share Posted May 20, 2021 2 hours ago, FrancescoDiMuro said: it leaves blank lines at the top of the string without too much of examining how, this somehow moves everything to the top "(User|Login-name|NTSecurity|Member):\s(.*)|(.+)\r\n?(?:\r?\n)" FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now