Jump to content

Take string until a char


Go to solution Solved by Malkey,

Recommended Posts

Posted

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

 

Posted

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 :D ) and i don't know if is correct or not

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Posted

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

Posted

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

Posted (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 by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted (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 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

Posted

 

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 :x

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...