Jump to content
Sign in to follow this  
Allow2010

some regex troubles

Recommended Posts

Allow2010

i am trying to parse some textinfo and it just seems not to work.

here you can see what i am trying to do

http://regexr.com?34r61

i want to find the state (active or not) for all ten devices.

the regexp

(?ms)["tam:settings/TAM/list(Active,Name,Display,MSNBitmap)"] = {.*[4] = {s*["Active"] = "([10])",

seems to work and i can get the needed result by changing the numer (in bold).

I tried this in autoit with

$tam=4

$state = StringRegExp($text, '["tam:settings/TAM/list(Active,Name,Display,MSNBitmap)"] = {s*[' & $tam & '] = {s*["Active"] = "([10])",', 1)

but i never get a result...

can anyone please help me:-)

Edited by Allow2010

Share this post


Link to post
Share on other sites
guinness

Where is that json syntax from?

Also look at the last parameter, change from 1 to 3.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
mikell

Try this

#include <Array.au3>

$text = FileRead("1.txt")

$state = StringRegExp($text, '(?s)Active"\] = "(\d)"', 3)
_ArrayDisplay($state)

Edit

Or this

#include <Array.au3>

$text = FileRead("1.txt")

$state = StringRegExp($text, '(?s)Active"\] = "(\d)"', 3)
Dim $array[UBound($state)+1][2] = [["device", "status"]]
For $i = 1 to UBound($state)
  $array[$i][0] = $i
  $array[$i][1] = $state[$i-1]
Next
_ArrayDisplay($array)
Edited by mikell

Share this post


Link to post
Share on other sites
Allow2010

Where is that json syntax from?

Also look at the last parameter, change from 1 to 3.

 

i am not sure what you mean by json syntax...

Share this post


Link to post
Share on other sites
Allow2010

 

Try this

 

thanks, i will try and report back...but (as the file holds some more text) it might be a bit too simple and may match something that looks similar..thats why i decided to match tam:settings/TAM/list(Active,Name,Display,MSNBitmap)"] first...

Share this post


Link to post
Share on other sites
mikell

Frequently, more complicated the expression is, more errors may occur

I based this regex on the text you provided with your link, but if you have to manage something different the sre must obviously be adapted

Share this post


Link to post
Share on other sites
Jury

Whatever but don't forget the powerful;

(?=[d+]) 

This asserts that this:

[d+])

 can be matched, then continues by starting at this position and so on until end of file Z in my script.

#include <Array.au3>

$text = FileRead(@MyDocumentsDir & "\test.txt")

$state = StringRegExp($text, '(?-s)\[\d+\].*?\r?\n.*?\["Active"\]\s*=\s*"(\d+)"(?s).*?(?=\[\d+\]|\Z)', 3)
Dim $array[UBound($state)+1][2] = [["device", "status"]]
For $i = 1 to UBound($state)
  $array[$i][0] = $i
  $array[$i][1] = $state[$i-1]
Next
_ArrayDisplay($array)
Edited by Jury
  • Like 1

Share this post


Link to post
Share on other sites
Allow2010

 

Whatever but don't forget the powerful;

(?=[d+]) 

This asserts that this:

[d+])

 can be matched, then continues by starting at this position and so on until end of file Z in my script.

#include <Array.au3>

$text = FileRead(@MyDocumentsDir & "\test.txt")

$state = StringRegExp($text, '(?-s)\[\d+\].*?\r?\n.*?\["Active"\]\s*=\s*"(\d+)"(?s).*?(?=\[\d+\]|\Z)', 3)
Dim $array[UBound($state)+1][2] = [["device", "status"]]
For $i = 1 to UBound($state)
  $array[$i][0] = $i
  $array[$i][1] = $state[$i-1]
Next
_ArrayDisplay($array)

Well, thank you...but i have to take a very close look at this to understand it...will reply when i understand :-)

Edited by Allow2010

Share this post


Link to post
Share on other sites
Allow2010

Meanwhile i have another regexp problem that gives me a headache:

<textarea id="uiDnsRebind" name="dns_rebind_list" cols="30" rows="5">Line1
Line2
Line3
morelines

</textarea>

 

i want the text between

<textarea id="uiDnsRebind" name="dns_rebind_list" cols="30" rows="5">

and </textarea>

and i can sucessfully get it with

$matches = StringRegExp($result, '(?ms)<textarea id="uiDnsRebind" name="dns_rebind_list" cols="30" rows="5">(.*?)</textarea>', 1)

but after that all the linefeeds are converted to the text "lf". Not sure why that is...

Can anyone tell me how i can get this as a result:

"Line1%0D%0ALine2%0D%0ALine3%0D%0Amorelines%0D%0A"

I am banging my head now for over an hour:-(

Edited by Allow2010

Share this post


Link to post
Share on other sites
mikell

I'm not sure of the result you exactly expect but please try this

$txt = '<textarea id="uiDnsRebind" name="dns_rebind_list" cols="30" rows="5">Line1' & @crlf & _ 
'Line2' & @crlf & _ 
'Line3' & @crlf & _ 
'morelines' & @crlf & _ 
'</textarea>'

$res = StringRegExp($txt, '(?s)<textarea.+?>(.*?)<', 3)
msgbox(0, "", $res[0])
$res2 = StringReplace($res[0], @crlf, '%0D%0A')
msgbox(0, "", $res2)

Share this post


Link to post
Share on other sites
Allow2010

Thank Your, it works fine...only problem i had was that for some reasons the text i am parsing used @LF instead of @CRLF. Once i found it all worked out...

Thanks again to everyone for the help...

Share this post


Link to post
Share on other sites
guinness

Rule of thumb >> Windows >> rn or @CRLF for line endings. It just stops problems like this happening.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
mikell

...for some reasons the text i am parsing used @LF instead of @CRLF. Once i found it all worked out...

You can solve this with regexp  :)

$txt = '<textarea id="uiDnsRebind" name="dns_rebind_list" cols="30" rows="5">Line1' & @crlf & _ 
'Line2' & @lf & _ 
'Line3' & @lf & _ 
'morelines' & @crlf & _ 
'</textarea>' 

;msgbox(0, "", $txt ) 
$res = StringRegExp($txt, '(?s)<textarea.+?>(.*?)<', 3) 
;msgbox(0, "", $res[0]) 
$res2 = StringRegExpReplace($res[0], @crlf & '|' & @lf, '%0D%0A')  ;<<<<<<
msgbox(0, "", $res2)

Share this post


Link to post
Share on other sites
kylomas

F.Y.I.

M23 came up with this slick assed SRE to normalize EOL's

; By Melba23, convert @CR & @LF to @CRLF. Using lookarounds, very clever!
$sReturn = StringRegExpReplace($sReturn, '((?<!\x0d)\x0a|\x0d(?!\x0a))', @CRLF)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
guinness

I need to ask why Melba23 why he opted for the hex values instead of r and n?

; By Melba23, convert @CR & @LF to @CRLF. Using lookarounds, very clever!
$sReturn = StringRegExpReplace($sReturn, '((?<!\r)\n|\r(?!\n)', @CRLF)

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×