kcvinu Posted March 11, 2015 Share Posted March 11, 2015 Hi all, How to find first and last word in a sentence with Regular expression ?. Sentence is the code from SciTE. For example if sentence is; "For $i = 0 to 15" I need to extract "For from the sentence And the same way i need last word from a sentence "If Apple = 15 Then" I need "Then" from the sentence. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) Link to comment Share on other sites More sharing options...
water Posted March 11, 2015 Share Posted March 11, 2015 The most important thing for a working solution is to define the meaning of "word". You need to define what delimits a word. Space, comma, bracket etc. kcvinu 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
mpower Posted March 11, 2015 Share Posted March 11, 2015 (edited) Hi all, How to find first and last word in a sentence with Regular expression ?. Sentence is the code from SciTE. For example if sentence is; "For $i = 0 to 15" I need to extract "For from the sentence And the same way i need last word from a sentence "If Apple = 15 Then" I need "Then" from the sentence. Give this a whirl: $sentence = "For $i = 0 to 15" $sentence2 = "If Apple = 15 Then" $firstword = StringRegExp($sentence, "(?:^|(?:\.\s))(\w+)", 1) $lastword = StringRegExp($sentence, "\s(\w+)$", 1) ConsoleWrite($firstword[0] & @CRLF) ConsoleWrite($lastword[0] & @CRLF) $firstword = StringRegExp($sentence2, "(?:^|(?:\.\s))(\w+)", 1) $lastword = StringRegExp($sentence2, "\s(\w+)$", 1) ConsoleWrite($firstword[0] & @CRLF) ConsoleWrite($lastword[0] & @CRLF) Sources: http://lmgtfy.com/?q=regex+first+word+in+sentence and http://lmgtfy.com/?q=regex+last+word+in+sentence Edited March 11, 2015 by mpower Link to comment Share on other sites More sharing options...
iamtheky Posted March 11, 2015 Share Posted March 11, 2015 $sentence = "For $i = 0 to 15" $sentence2 = "If Apple = 15 Then" msgbox( 0 , '' , _firstlast($sentence)) msgbox( 0 , '' , _firstlast($sentence2)) Func _firstlast($string) $aString = stringsplit($string , " ") return $aString[1] & @CRLF & $aString[ubound($aString) - 1] EndFunc ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
kcvinu Posted March 11, 2015 Author Share Posted March 11, 2015 I have googled a lot and tried something with the RegExp_GUI. Thanks for AutoIt for giving me a nice program for testing regular expresions. @Water, In this case a word which separated with space only. @boththose and @mpower, thanks. Let me try. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) Link to comment Share on other sites More sharing options...
Solution jguinch Posted March 11, 2015 Solution Share Posted March 11, 2015 (edited) #Include <Array.au3> $sentence2 = "If Apple = 15 Then" $words = StringRegExp($sentence2, "(?m)\W*(\w+).*?(\w+)\W*$", 1) _ArrayDisplay($words) Here is a small explanation : (?m) : multiline mode. With this mode, ^ and $ operates on each line instead of the whole stringW* : any non-word character, 0 or more times(w+) : capturing group. any word character, one or more times.*? : anything, one or more times. ? takes the smallest occurence $ : end of line or end of string Edited March 11, 2015 by jguinch kcvinu 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
kcvinu Posted March 11, 2015 Author Share Posted March 11, 2015 @ jguinch, Thanks. There is more than one anser which needs to mark solved. What to do. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) Link to comment Share on other sites More sharing options...
water Posted March 11, 2015 Share Posted March 11, 2015 Mark the one which is easiest for you to underrstand. kcvinu 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
kcvinu Posted March 11, 2015 Author Share Posted March 11, 2015 @water, Actually regular expression code is always horrible for me. So there is no easiest thing. I have googled a lot for what this "?" stands for . I didn't get any proper answer. So i have downloaded O'reilly's Regular expression Nutshell. And started reading. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) Link to comment Share on other sites More sharing options...
jguinch Posted March 11, 2015 Share Posted March 11, 2015 kcvinu, I edited my code to add an explanation... kcvinu 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
mikell Posted March 11, 2015 Share Posted March 11, 2015 (edited) If you feel bad with regex, why don't you use the code from boththose ? It works nice - if you just add a StringStripWS $sentence = " If Apple = 15 Then " msgbox( 0 , "" , _firstlast($sentence)) Func _firstlast($string) $aString = stringsplit(StringStripWS($string, 3) , " ") return $aString[1] & @CRLF & $aString[$aString[0]] EndFunc Edit BTW in the case below all the codes on this page will fail $sentence = " If Apple = 15 Then ; this is a condition " Edited March 11, 2015 by mikell kcvinu 1 Link to comment Share on other sites More sharing options...
kcvinu Posted March 11, 2015 Author Share Posted March 11, 2015 @ mikell, I think RegExp code works faster. And i had a wish to learn regular expression. May be it is harder. But i need to learn it. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) Link to comment Share on other sites More sharing options...
mikell Posted March 11, 2015 Share Posted March 11, 2015 So... #Include <Array.au3> $sentence = " If Apple = 15 Then ; this is a condition " $words = StringRegExp($sentence, "\s*(\w+)[^;]+\s(\w+);?.*", 3) _ArrayDisplay($words) kcvinu 1 Link to comment Share on other sites More sharing options...
kcvinu Posted March 11, 2015 Author Share Posted March 11, 2015 @ mikell, So either Regular expression is for humans or you are an alien. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) Link to comment Share on other sites More sharing options...
mikell Posted March 11, 2015 Share Posted March 11, 2015 Not an alien (so thinks my wife) but I've been a regex student for a long time - and I still am, and always will be kcvinu 1 Link to comment Share on other sites More sharing options...
kcvinu Posted March 11, 2015 Author Share Posted March 11, 2015 @mikell Then please suggest me a good and free e-book or pdf book to learn regular expression. I am complete beginner. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 11, 2015 Moderators Share Posted March 11, 2015 kcvinu,I always recommend this site. But the learning curve is steep and stays that way - when mikell says "I've been a regex student for a long time " he really means it! M23 kcvinu 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
kcvinu Posted March 11, 2015 Author Share Posted March 11, 2015 @Melba23 , Thank you. I know that. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) Link to comment Share on other sites More sharing options...
mikell Posted March 11, 2015 Share Posted March 11, 2015 I have 2 favorite sites : the one Melba mentioned, and this other one http://www.rexegg.com/ On both sites explanations are clear and understandable - very important condition when dealing with regex kcvinu 1 Link to comment Share on other sites More sharing options...
kcvinu Posted March 11, 2015 Author Share Posted March 11, 2015 @Mikell, Thanks. Spoiler My Contributions Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language. UDF Link Viewer --- A tool to visit the links of some most important UDFs Includer_2 ----- A tool to type the #include statement automatically Digits To Date ----- date from 3 integer values PrintList ----- prints arrays into console for testing. Alert ------ An alternative for MsgBox MousePosition ------- A simple tooltip display of mouse position GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function Access_UDF -------- An UDF for working with access database files. (.*accdb only) 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