AlienStar Posted August 15, 2020 Share Posted August 15, 2020 hello everybody I need to search in txt file contains like this : categ_1=Sun|123 categ_1=Sun light|222 if I search Local $sCase = "" Local $skeyword = "category_1="&"Sun" Local $Rslt_Site = StringRegExp(FileRead($Site_db), $sCase & "(?m:^).*" & $skeyword & ".*(?:\v|$)+", 3) it returns array contains the both lines (Sun|123 , Sun light|222) I tried to change keyword into : $skeyword = "categ_1="&"Sun"&"|" but it returns all file contents in an array with some changes what can I do to search in case-sensitive please ? thanks Link to comment Share on other sites More sharing options...
TheXman Posted August 15, 2020 Share Posted August 15, 2020 (edited) 43 minutes ago, AlienStar said: I tried to change keyword into : $skeyword = "categ_1="&"Sun"&"|" All regular expression searches are case-sensitive, unless explicitly told not to be. Your issue has NOTHING to do with case-sensitivity. It appears that you aren't aware that the pipe symbol ("|"), as well as the following characters "[\^$.?*+(){}", have special meanings in regular expressions. In general, if you want them to be interpreted literally, then you need to escape them. When discussing regular expressions, it is always helpful to say exactly what the expected result should be as well as an accurate depiction of the input data. In this case, what is the result that you are expecting by changing the $sKeyword to "categ_1="&"Sun"&"|"? Edited August 15, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 26 minutes ago, TheXman said: All regular expression searches are case-sensitive, unless explicitly told not to be. Your issue has NOTHING to do with case-sensitivity. It appears that you aren't aware that the pipe symbol ("|"), as well as the following characters "[\^$.?*+(){}", have special meanings in regular expressions. In general, if you want them to be interpreted literally, then you need to escape them. When discussing regular expressions, it is always helpful to say exactly what the expected result should be as well as an accurate depiction of the input data. In this case, what is the result that you are expecting by changing the $sKeyword to "categ_1="&"Sun"&"|"? I have some names are linked to specific numbers So I need to search for "Sun" to get its number "123" only in the search array without getting multi items in the array like this [0] categ_1=Sun|123 [1] categ_1=Sun light|222 So I need to get one item in the search array Link to comment Share on other sites More sharing options...
mikell Posted August 15, 2020 Share Posted August 15, 2020 55 minutes ago, AlienStar said: I need to search in txt file When using regex, you must define precisely the requirements. What is exactly the expected result in such a case ? $txt = "categ_1=Sun|123" & @crlf & _ "categ_1=Sun light|222" & @crlf & _ "categ_1=Sundance|69" & @crlf & _ "categ_1=Maui Sun|69" Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 (edited) 4 minutes ago, mikell said: When using regex, you must define precisely the requirements. What is exactly the expected result in such a case ? $txt = "categ_1=Sun|123" & @crlf & _ "categ_1=Sun light|222" & @crlf & _ "categ_1=Sundance|69" & @crlf & _ "categ_1=Maui Sun|69" When I wanna search for : $skeyword = "categ_1="&"Sun" I wanna get just this item in the search array : [0] categ_1=Sun|123 but up to now I get this: [0] categ_1=Sun|123 [1] categ_1=Sun light|222 Edited August 15, 2020 by AlienStar Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 (edited) I can't using the first item in this array to get what I need [0] categ_1=Sun|123 [1] categ_1=Sun light|222 because in many cases the name I need to search for is not first item in the search array Edited August 15, 2020 by AlienStar Link to comment Share on other sites More sharing options...
TheXman Posted August 15, 2020 Share Posted August 15, 2020 (edited) Maybe something like this? #include <Array.au3> example() Func example() Const $DATA = "categ_1=Sun|123" & @crlf & _ "categ_1=Sun light|222" & @crlf & _ "categ_1=Sundance|69" & @crlf & _ "categ_1=Maui Sun|69" Local $sKeyword = "" Local $aResult[0] $sKeyword = "categ_1=" & "Sun|" $aResult = StringRegExp($DATA, "(?m)^\Q" & $skeyword & "\E.*", 3) If IsArray($aResult) Then _ArrayDisplay($aResult, $sKeyword) $sKeyword = "categ_1=Sun" $aResult = StringRegExp($DATA, "(?m)^\Q" & $skeyword & "\E.*", 3) If IsArray($aResult) Then _ArrayDisplay($aResult, $sKeyword) $sKeyword = "categ_1=Sun light" $aResult = StringRegExp($DATA, "(?m)^\Q" & $skeyword & "\E.*", 3) If IsArray($aResult) Then _ArrayDisplay($aResult, $sKeyword) $sKeyword = "categ_1=Maui" $aResult = StringRegExp($DATA, "(?m)^\Q" & $skeyword & "\E.*", 3) If IsArray($aResult) Then _ArrayDisplay($aResult, $sKeyword) EndFunc Edited August 15, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 3 minutes ago, TheXman said: Maybe something like this? #include <Constants.au3> #include <Array.au3> example() Func example() Const $DATA = "categ_1=Sun|123" & @crlf & _ "categ_1=Sun light|222" & @crlf & _ "categ_1=Sundance|69" & @crlf & _ "categ_1=Maui Sun|69" Local $sKeyword = "" Local $aResult[0] $sKeyword = "categ_1=Sun|" $aResult = StringRegExp($DATA, "(?m).*?\Q" & $skeyword & "\E.*", 3) If IsArray($aResult) Then _ArrayDisplay($aResult) $sKeyword = "categ_1=Sun" $aResult = StringRegExp($DATA, "(?m).*?\Q" & $skeyword & "\E.*", 3) If IsArray($aResult) Then _ArrayDisplay($aResult) $sKeyword = "categ_1=Sun ligh" $aResult = StringRegExp($DATA, "(?m).*?\Q" & $skeyword & "\E.*", 3) If IsArray($aResult) Then _ArrayDisplay($aResult) EndFunc by your syntax $sKeyword = "categ_1=Sun|" it works but it doesn't work by my syntax : $skeyword = "categ_1="&"Sun" because "1" after "categ_" is a value and "Sun" also Link to comment Share on other sites More sharing options...
TheXman Posted August 15, 2020 Share Posted August 15, 2020 (edited) 7 minutes ago, AlienStar said: by your syntax $sKeyword = "categ_1=Sun|" it works but it doesn't work by my syntax : $skeyword = "categ_1="&"Sun" because "1" after "categ_" is a value and "Sun" also I have no idea what you are talking about. First of all, the 2 lines above are not even the same. One has a "|" and the other does not. This works just fine: $sKeyword = "categ_1=" & "Sun|" Edited August 15, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 Just now, TheXman said: I have no idea what you are talking about. First of all, the 2 lines above are not even the same. One has a "|" and the other does not. I accept with you at all but I don't know why the result changes when using $skeyword = "categ_1="&"Sun" !!!! Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 the keyword I need to use in my code is like this $skeyword = "categ_" & $id & "=" & $categ_nm Link to comment Share on other sites More sharing options...
TheXman Posted August 15, 2020 Share Posted August 15, 2020 3 minutes ago, AlienStar said: the keyword I need to use in my code is like this Then modify the example to meet your needs. I have pointed you in the right direction. I'm not going to pick you up and carry you all the way to your destination by giving you a fully working solution. CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 I thank you for your help but I don't know why the search result changes when changing $skeyword although it's same in two cases Link to comment Share on other sites More sharing options...
TheXman Posted August 15, 2020 Share Posted August 15, 2020 (edited) 1 hour ago, AlienStar said: I accept with you at all but I don't know why the result changes when using Regular expressions, as with any other programming language, do exactly what you tell them to do. The difference between the 2 keyword examples is one looks for all lines that start with "...Sun" and the other looks for all lines starting with "...Sun|" . In the example, 3 lines match the first regular expression and only one line matches the second regular expression. That is why when discussing regular expression it is SO IMPORTANT to describe, in as much detail as possible, the input data, the matching criteria, AND the expected results, otherwise unexpected results may occur. Edited August 15, 2020 by TheXman AlienStar 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
mikell Posted August 15, 2020 Share Posted August 15, 2020 ... what about this ? #Include <Array.au3> $txt = "categ_1=Sun light|123" & @crlf & _ "categ_1=Sun|222" & @crlf & _ "categ_1=Sundance|69" & @crlf & _ "categ_1=Maui Sun|69" $n = 1 $skeyword1 = "categ_" $skeyword2 = "Sun" $res = StringRegExp($txt, $skeyword1 & $n & '=(' & $skeyword2 & ')\|(\d+)', 3) _ArrayDisplay($res) AlienStar 1 Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 (edited) 44 minutes ago, mikell said: ... what about this ? #Include <Array.au3> $txt = "categ_1=Sun light|123" & @crlf & _ "categ_1=Sun|222" & @crlf & _ "categ_1=Sundance|69" & @crlf & _ "categ_1=Maui Sun|69" $n = 1 $skeyword1 = "categ_" $skeyword2 = "Sun" $res = StringRegExp($txt, $skeyword1 & $n & '=(' & $skeyword2 & ')\|(\d+)', 3) _ArrayDisplay($res) it works that's what I need thanks so much Edited August 15, 2020 by AlienStar Link to comment Share on other sites More sharing options...
TheXman Posted August 15, 2020 Share Posted August 15, 2020 (edited) 1 hour ago, AlienStar said: the keyword I need to use in my code is like this $skeyword = "categ_" & $id & "=" & $categ_nm If you wanted something like this, then why didn't you say that in the beginning? It would have save at least 2 rounds of playing "whack-a-mole" because it gives a much better idea of what you are ultimately trying to do. Since you are having a hard time understanding how regular expressions work, I offer one more example that is closer to what you are looking for. #include <Constants.au3> #include <Array.au3> example() Func example() Const $DATA = "categ_1=Sun|123" & @crlf & _ "categ_1=Sun light|222" & @crlf & _ "categ_1=Sundance|69" & @crlf & _ "categ_1=Maui Sun|69" & @CRLF & _ "categ_2=Sun light|33" & @crlf & _ "categ_2=Sundance|44" & @crlf & _ "categ_2=Maui Sun|55" Local $sId = "", $sCategory = "" Local $aResult[0] $sId = "1" $sCategory = "Sun" $aResult = StringRegExp($DATA, StringFormat("(?im)^categ_%s=%s\|.*", $sId, $sCategory), $STR_REGEXPARRAYGLOBALMATCH) If @error Then Switch @error Case 1 ;No Matches MsgBox($MB_ICONWARNING, "WARNING", "No matches found.") Return Case 2 ;Bad RegEx MsgBox($MB_ICONWARNING, "WARNING", "Regular expression error at offset " & @extended) Exit 1 EndSwitch EndIf _ArrayDisplay($aResult) EndFunc Edited August 15, 2020 by TheXman AlienStar 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 3 minutes ago, TheXman said: If you wanted something like this, then why didn't you say that in the beginning? It would have save at least 2 rounds of playing "whack-a-mole" because it gives a much better idea of what you are ultimately trying to do. Since you are having a hard time understanding how regular expressions work, I offer one more example that is closer to what you are looking for. #include <Constants.au3> #include <Array.au3> example() Func example() Const $DATA = "categ_1=Sun|123" & @crlf & _ "categ_1=Sun light|222" & @crlf & _ "categ_1=Sundance|69" & @crlf & _ "categ_1=Maui Sun|69" & @CRLF & _ "categ_2=Sun light|33" & @crlf & _ "categ_2=Sundance|44" & @crlf & _ "categ_2=Maui Sun|55" Local $sId = "", $sCategory = "" Local $aResult[0] $sId = "1" $sCategory = "Sun" $aResult = StringRegExp($DATA, StringFormat("(?im)^categ_%s=%s\|.*", $sId, $sCategory), $STR_REGEXPARRAYGLOBALMATCH) If @error Then Switch @error Case 1 ;No Matches MsgBox($MB_ICONWARNING, "WARNING", "No matches found.") Return Case 2 ;Bad RegEx MsgBox($MB_ICONWARNING, "WARNING", "Regular expression error at offset " & @extended) Exit 1 EndSwitch EndIf _ArrayDisplay($aResult) EndFunc sorry for bad expression at first and thanks so much it's working Link to comment Share on other sites More sharing options...
TheXman Posted August 15, 2020 Share Posted August 15, 2020 You're welcome! AlienStar 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
mikell Posted August 16, 2020 Share Posted August 16, 2020 9 hours ago, TheXman said: why didn't you say that in the beginning? It's a very usual problem, reason why when a user asks for help about regex the first answer is at 99% usually : please define precisely the requirements and the expected result(s) 9 hours ago, AlienStar said: it works that's what I need thanks so much Great. But please keep in mind that - the results to grab must be enclosed by parenthesis - in my snippet $skeyword2 MUST be preceded by "=" and followed by "|" and the number to get. If ANY additional requirement is added then the pattern must be modified AlienStar 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