Jump to content

Regular Expression Help


Teldin
 Share

Recommended Posts

How would I write an expression that reads the first 2 words of a sentence? I would also like to stop at a certain word "STOP" if it's one of the first two.

For example:

"this sentence is a test"

the regex would grab "this sentence"

but

"now stop at the second word"

would only get "now" as stop is the word to stop at.

Thanks in advance.

Link to comment
Share on other sites

  • Moderators

How would I write an expression that reads the first 2 words of a sentence? I would also like to stop at a certain word "STOP" if it's one of the first two.

For example:

"this sentence is a test"

the regex would grab "this sentence"

but

"now stop at the second word"

would only get "now" as stop is the word to stop at.

Thanks in advance.

Well ... here's a silly question. Are you asking about this because you understand Regular Expressions a bit, but are confused on how to find the first 2 words of a sentence. Or are you only wanting someone to give you the answer so you don't have to try it on your own? I ask this, because I see no attempts on your behalf to even try to use StringRegExp().

Here's a helpful site:

http://perldoc.perl.org/perlre.html#Regular-Expressions

I've also (recently) posted in a Reg Exp here on how to find each word of a string. No idea where it is, but if you don't want to learn... you could always track it down :whistle: .

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Yes I have read that and these from my favorites:

http://regexlib.com/CheatSheet.aspx

http://www.javascriptkit.com/jsref/regexp.shtml

http://www.codeproject.com/dotnet/regextutorial.asp

I have tried to figure it out but having a hard time so I post here for help.

I have downloaded Expresso, Waterproof, Sells Bothers, the forums' own StringRegExp GUI, and several other RegEx apps to learn RegEx but it's a foreign language.

I was able to grab the first word but not the first two nor could I mange to stop at a certian word.

Right now I have:

$Pla = StringRegExp(GUICtrlRead($Input), "(?im)(^..........)", 3 )

which grabs the first 10 chars of a string but does not achieve exactly what I want.

Link to comment
Share on other sites

  • Moderators

#include <array.au3>
$sString = "What are the words of this question?"
$aArray = StringRegExp($sString, "[\s\.:;,]*([a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
_ArrayDisplay($aArray, '')

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You want to do this only using RegExp? you can do it without using it:

$String = "test test1 test2... test3"

$TwoWords = _GetWords($String, 2)

MsgBox(0, "", $TwoWords)

Func _GetWords($String, $WordsCount)
    If Not StringInStr($String, " ") Or Not IsNumber($WordsCount) Then Return $String
    Local $Arr = StringSplit($String, " ", 1), $Ret, $i
    If $Arr[0] >= $WordsCount Then
        For $i = 1 To $WordsCount
            $Ret &= " " & $Arr[$i]
        Next
        Return $Ret
    Else
        Return $String
    EndIf
EndFunc
Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • Moderators

I want to do this only using RegExp? you can do it without using it:

$String = "test test1 test2... test3"

$TwoWords = _GetWords($String, 2)

MsgBox(0, "", $TwoWords)

Func _GetWords($String, $WordsCount)
    If Not StringInStr($String, " ") Or Not IsNumber($WordsCount) Then Return $String
    Local $Arr = StringSplit($String, " ", 1), $Ret, $i
    If $Arr[0] >= $WordsCount Then
        For $i = 1 To $WordsCount
            $Ret &= " " & $Arr[$i]
        Next
        Return $Ret
    Else
        Return $String
    EndIf
EndFuncoÝ÷ Ûú®¢×+¡×¢²)Ü¢jez×¢íý­æ¬²é¢ajׯz¼°¢·b²åÊÇ©j¶­yÖòjÊZqáÒ'i×b¶*'Ü"X§r[y©e¦éܶæ­ëº{aÊ'Ö®¶­sbb33cµ7G&ærÒgV÷C·FW7BÂFW7CFW7C"âââFW7C2gV÷C° ¢b33cµGvõv÷&G2ÒôvWEv÷&G2b33cµ7G&ærÂ" ¤×6t&÷ÂgV÷C²gV÷C²Âb33cµGvõv÷&G2 ¤gVæ2ôvWEv÷&G2b33cµ7G&ærÂb33cµv÷&G46÷VçB bæ÷B7G&ætå7G"b33cµ7G&ærÂgV÷C²gV÷C²÷"æ÷B4çVÖ&W"b33cµv÷&G46÷VçBFVâ&WGW&âb33cµ7G&æp Æö6Âb33c´'"Ò7G&æu7ÆBb33cµ7G&ærÂgV÷C²gV÷C²ÂÂb33cµ&WBÂb33c¶ bb33c´'%³ÒfwC³Òb33cµv÷&G46÷VçBFVà f÷"b33c¶ÒFòb33cµv÷&G46÷Vç@ b33cµ&WBf׳ÒgV÷C²gV÷C²fײb33c´'%²b33c¶Ð æW@ &WGW&âb33cµ&W@ VÇ6P &WGW&âb33cµ7G&æp VæD`¤VæDgVæ0
Also in the long run, if the text you are reading is large, your function will like 2000 times slower (literally) than the StringRegExp.
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

SmOke_N

You're assuming that every word is only separated by a space

Yes, i dont think that the : , or even . is for separate the words...

And if you run RegExp on this:

$String = "test:test1,,,test2...test3"

Then you will get 4 words, but maby i do want the : as part of my first word :whistle: ...

About the speed, yes, it probably be much slower (my function).

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • Moderators

SmOke_N

Yes, i dont think that the : , or even . is for separate the words...

And if you run RegExp on this:

$String = "test:test1,,,test2...test3"

Then you will get 4 words, but maby i do want the : as part of my first word :whistle: ...

About the speed, yes, it probably be much slower (my function).

You can mod the regexp however you like... I did/do however like how you have (in this thread and others) posted different types of solutions... albeit better or not.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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