Daydreamer067 Posted March 2, 2021 Share Posted March 2, 2021 Hi I want to parse a string to return all the words separated by space. "string exemple" would return string and exemple. I tried with StringRegExp but I don't know how to make it work. Your help is appreciated. Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted March 2, 2021 Share Posted March 2, 2021 @Daydreamer067 Maybe something like this? #include <Array.au3> #include <StringConstants.au3> Test() Func Test() Local $arrResult $arrResult = StringRegExp("This is a test string with spaces." & @CRLF & _ " Even with leading speaces, double spaces, or trailing spaces .", _ '(\b\w+\b)', _ $STR_REGEXPARRAYGLOBALMATCH) _ArrayDisplay($arrResult) EndFunc AspirinJunkie, wolflake and Earthshine 3 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...
AspirinJunkie Posted March 2, 2021 Share Posted March 2, 2021 It doesn't always have to be regex: #include <Array.au3> $sString = "string exemple" $aSplit = StringSplit(StringStripWS($sString, 7), " ", 2) _ArrayDisplay($aSplit) FrancescoDiMuro and Earthshine 2 Link to comment Share on other sites More sharing options...
mikell Posted March 2, 2021 Share Posted March 2, 2021 2 hours ago, AspirinJunkie said: It doesn't always have to be regex: Not false. But you grab the final dot too, and there is an issue (for me) with the string provided by @FrancescoDiMuro FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
AspirinJunkie Posted March 2, 2021 Share Posted March 2, 2021 1 hour ago, mikell said: But you grab the final dot too How does the final point relate to the thread creator's task? Link to comment Share on other sites More sharing options...
mikell Posted March 2, 2021 Share Posted March 2, 2021 4 hours ago, AspirinJunkie said: How does the final point relate to the thread creator's task? Your code returns the final dot as a valid result The question was about 'return all the words' so I doubt the OP could be interested in a dot FrancescoDiMuro and Earthshine 2 Link to comment Share on other sites More sharing options...
AspirinJunkie Posted March 2, 2021 Share Posted March 2, 2021 (edited) The only information about the source we have is: "words separeted by space". Nothing read about a point or anything else. Especially if no point at all appears in the example of the questioner. The point was first introduced by Francesco. So up to here, there is no reason to cover points extra. Therefore, it does not make much sense to extend the task further than it was set. Otherwise, one could also say that Francesco's code also extracts words that are not separated by spaces but only by semicolons. For the task given so far, however, his code fulfills the task completely correctly and is sufficient until Daydreamer specifies the task further. Edited March 2, 2021 by AspirinJunkie Earthshine 1 Link to comment Share on other sites More sharing options...
Musashi Posted March 2, 2021 Share Posted March 2, 2021 41 minutes ago, AspirinJunkie said: ... is sufficient until Daydreamer specifies the task further. I would like to agree with @AspirinJunkie regarding this issue. The general conditions that the questioner has mentioned are insufficient for a more precise answer. Crystal balls are quite rare in our times . Earthshine 1 "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
Earthshine Posted March 2, 2021 Share Posted March 2, 2021 (edited) different strokes and all i like both methods, and to tell the truth, my regexp fu is weak Edited March 2, 2021 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
mikell Posted March 2, 2021 Share Posted March 2, 2021 1 hour ago, AspirinJunkie said: The only information about the source we have is In fact we have (spaces excepted) no information at all... 'parsing string' is very vague. Is this source string a text ? How should we parse "AutoIt v3 is a freeware BASIC-like scripting language." ? In such cases as usual we try to guess (maybe wrongly) what could be inside the subject 'string' Link to comment Share on other sites More sharing options...
JockoDundee Posted March 2, 2021 Share Posted March 2, 2021 52 minutes ago, mikell said: In such cases as usual we try to guess (maybe wrongly) what could be inside the subject 'string' fair enough, however what we use to try and guess is strongly influenced by what is presented, in this case: 11 hours ago, Daydreamer067 said: "string exemple" would return string and exemple. which, while admittedly offering little to go on, does not appear to be a sentence. furthermore, at this point the OP doesn’t seem concerned with any subtle details: 11 hours ago, Daydreamer067 said: I tried with StringRegExp but I don't know how to make it work. and although he mentions specifically StringRegEx in his question, the answer need not use such. at this point though, I think the OP has been more than adequately served by all Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted March 2, 2021 Share Posted March 2, 2021 And this, ladies and gentlemen (I said gentlemen, so regex cats are not included), it's how a simple question become the official thread of "word or not word, this is the question" Spoiler For the regex cat @mikell, just kidding. I love every regex pattern you post mikell 1 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...
JockoDundee Posted March 3, 2021 Share Posted March 3, 2021 unless the OP returns, I’m thinking that we’ve been expertly trolled by someone who, (being aware of the community’s bitter divide between those who RegEx and those who do not), has served up a perfectly ambiguous, right-down-the-middle-of-the-road question for us to squabble over, while he smugly sits back; chuckling as he mumbles “exemple”. Danp2 and FrancescoDiMuro 2 Code hard, but don’t hard code... 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