Terenz Posted September 20, 2014 Share Posted September 20, 2014 Hello again, If i have a string like: 321321bsidaybsdiaasdb-93218y4n9fnsdudn And i want to take the first part until -, what Func or RegExp i can use? I mean without StringSplit or _StringExplode, i don't need an array of value but only the first match Thanks Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
kylomas Posted September 20, 2014 Share Posted September 20, 2014 Simnplest way with array access on expression... local $str = '321321bsidaybsdiaasdb-93218y4n9fnsdudn' ConsoleWrite(stringsplit($str,'-',2)[0] & @CRLF) 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 Link to comment Share on other sites More sharing options...
Terenz Posted September 20, 2014 Author Share Posted September 20, 2014 Yes but there is a possibility if the string don't contain any - so i need to check if is array of not, instead if is a string if the - is found trim the string: $String = "321321bsidaybsdiaasdb-93218y4n9fnsdudn" $Test = StringRegExpReplace($String, "^([^-]*)-", "") ConsoleWrite($Test & @CRLF) $String = "321321bsidaybsdiaasdb93218y4n9fnsdudn" $Test = StringRegExpReplace($String, "^([^-]*)-", "") ConsoleWrite($Test & @CRLF) If not post all the string. My regExp take the second part ( i need the first ) and i don't know if is correct or not Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
kylomas Posted September 20, 2014 Share Posted September 20, 2014 Any other requirements??? Try this... local $str1 = '321321bsidaybsdiaasdb-93218y4n9fnsdudn' local $str2 = '321321bsidaybsdiaasdb93218y4n9fnsdudn' ConsoleWrite(stringregexpreplace($str1,'([^-]+)(-.*)','\1') & @CRLF) ConsoleWrite(stringregexpreplace($str2,'([^-]+)(-.*)','\1') & @CRLF) 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 Link to comment Share on other sites More sharing options...
kylomas Posted September 20, 2014 Share Posted September 20, 2014 Second group does not need to be capturing... local $str1 = '321321bsidaybsdiaasdb-93218y4n9fnsdudn' local $str2 = '321321bsidaybsdiaasdb93218y4n9fnsdudn' ConsoleWrite(stringregexpreplace($str1,'([^-]+)-.*','\1') & @CRLF) ConsoleWrite(stringregexpreplace($str2,'([^-]+)-.*','\1') & @CRLF) 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 Link to comment Share on other sites More sharing options...
Terenz Posted September 20, 2014 Author Share Posted September 20, 2014 Thanks Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
kylomas Posted September 20, 2014 Share Posted September 20, 2014 NP, Good Luck 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 Link to comment Share on other sites More sharing options...
Gianni Posted September 20, 2014 Share Posted September 20, 2014 (edited) Second group does not need to be capturing... local $str1 = '321321bsidaybsdiaasdb-93218y4n9fnsdudn' local $str2 = '321321bsidaybsdiaasdb93218y4n9fnsdudn' ConsoleWrite(stringregexpreplace($str1,'([^-]+)-.*','\1') & @CRLF) ConsoleWrite(stringregexpreplace($str2,'([^-]+)-.*','\1') & @CRLF) this string "-321321bsidaybsdiaasdb-93218y4n9fnsdudn' returns the whole string, shouldn't it return an empty string? edit: I mean this string: "-321321bsidaybsdiaasdb93218y4n9fnsdudn' only one "-" at the beginning p.s. here my 2 cent: Local $str1 = '-321321bsidaybsdiaasdb93218y4n9fnsdudn' Local $str2 = '321321bsidaybsdiaas-db93218y4n9fnsdudn' Local $str3 = '321321bsidaybsdiaasdb93218y4n9fnsdudn' ConsoleWrite(StringMid($str1, 1, StringInStr($str1, "-") - 1) & @CR) ConsoleWrite(StringMid($str2, 1, StringInStr($str2, "-") - 1) & @CR) ConsoleWrite(StringMid($str3, 1, StringInStr($str3, "-") - 1) & @CR) Edited September 20, 2014 by Chimp Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
kylomas Posted September 20, 2014 Share Posted September 20, 2014 (edited) this string "-321321bsidaybsdiaasdb-93218y4n9fnsdudn' returns the whole string, shouldn't it return an empty string? edit: I mean this string: "-321321bsidaybsdiaasdb93218y4n9fnsdudn' only one "-" at the beginning p.s. here my 2 cent: Actually it returns the whole string because there is no match for the replace. @Terenz - Do you need to account for a single "-" at the beginning of the string? edit: @Chimp - For this simple string a non SRE method like yours or like this is fine... local $str1 = '321321bsidaybsdiaasdb-93218y4n9fnsdudn' local $str2 = '321321bsidaybsdiaasdb93218y4n9fnsdudn' local $str3 = '-321321bsidaybsdiaasdb93218y4n9fnsdudn' ConsoleWrite(stringsplit($str1,'-',2)[0] & @CRLF) ConsoleWrite(stringsplit($str2,'-',2)[0] & @CRLF) ConsoleWrite(stringsplit($str3,'-',2)[0] & @CRLF) Edited September 20, 2014 by 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 Link to comment Share on other sites More sharing options...
Solution Malkey Posted September 20, 2014 Solution Share Posted September 20, 2014 And this. Local $str1 = '321321bsidaybsdiaasdb-93218y4n9fnsdudn' Local $str2 = '321321bsidaybsdiaasdb93218y4n9fnsdudn' ConsoleWrite(StringRegExpReplace($str1, '-.*', '') & @CRLF) ; Returns 321321bsidaybsdiaasdb ConsoleWrite(StringRegExpReplace($str2, '-.*', '') & @CRLF) ; Returns 321321bsidaybsdiaasdb93218y4n9fnsdudn Link to comment Share on other sites More sharing options...
kylomas Posted September 20, 2014 Share Posted September 20, 2014 And this. Local $str1 = '321321bsidaybsdiaasdb-93218y4n9fnsdudn' Local $str2 = '321321bsidaybsdiaasdb93218y4n9fnsdudn' ConsoleWrite(StringRegExpReplace($str1, '-.*', '') & @CRLF) ; Returns 321321bsidaybsdiaasdb ConsoleWrite(StringRegExpReplace($str2, '-.*', '') & @CRLF) ; Returns 321321bsidaybsdiaasdb93218y4n9fnsdudn I was jerking around with something like that yesturday but managed to confuse myself by over-thinking the pattern 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 Link to comment Share on other sites More sharing options...
Terenz Posted September 20, 2014 Author Share Posted September 20, 2014 Malkey, Short version is always better, thanks Nothing is so strong as gentleness. Nothing is so gentle as real strength 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