Tutorial RegularExpressions: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(+Category:Tutorials)
 
(3 intermediate revisions by the same user not shown)
Line 13: Line 13:
==Collection of working samples==
==Collection of working samples==
===Extracting words:===
===Extracting words:===
<pre>
<syntaxhighlight lang="autoit">
$a = StringRegExp("Some words to extract", "[\w]+", 3)
$a = StringRegExp("Some words to extract", "[\w]+", 3)
_ArrayDisplay($a, "Extracted words")
_ArrayDisplay($a, "Extracted words")
</pre>
</syntaxhighlight>
 
===Extracting lines:===
===Extracting lines:===
<pre>
<syntaxhighlight lang="autoit">
$a = StringRegExp("Some words to extract", "(.*)", 3)
$a = StringRegExp("Some words to extract", "(.*)", 3)
_ArrayDisplay($a, "Extracted lines")
_ArrayDisplay($a, "Extracted lines")
</pre>
</syntaxhighlight>
 
===Extracting comments:===
===Extracting comments:===
===Extracting comment groups:===
===Extracting comment groups:===
Line 29: Line 31:
-JRowe
-JRowe


<pre>
<syntaxhighlight lang="autoit">
Local $a  
Local $a  
$a = StringRegExp($data, "(['""\w\d\(\),\;\:\-\@\&\s]+(?:.\w+)?+[\.\!\?])", 3)
$a = StringRegExp($data, "(['""\w\d\(\),\;\:\-\@\&\s]+(?:.\w+)?+[\.\!\?])", 3)
_ArrayDisplay($a, "Extracted lines")
_ArrayDisplay($a, "Extracted lines")
</pre>
</syntaxhighlight>


===Removing html tags:===
===Removing html tags:===
Line 39: Line 41:
Collect Global, Local and Dim statements in selected code and create declaration code to be inserted as a group in the file
Collect Global, Local and Dim statements in selected code and create declaration code to be inserted as a group in the file
TODO: Not tested!
TODO: Not tested!
<pre>
<syntaxhighlight lang="autoit">
Local $a  
Local $a  
$a = StringRegExp($data, "\s*(Global|Local|Dim)(\s+$\w+)", 3)
$a = StringRegExp($data, "\s*(Global|Local|Dim)(\s+$\w+)", 3)
_ArrayDisplay($a, "Extracted lines")
_ArrayDisplay($a, "Extracted lines")
</pre>
</syntaxhighlight>
<pre>
 
</pre>
==Other resources==
==Other resources==
Links to other resources or forum topics regarding the use of regular expressions in AutoIt.
Links to other resources or forum topics regarding the use of regular expressions in AutoIt.
Forum beginners tutorial by (TODO: @??? can't find it at the moment), really nice and well written.
Forum beginners tutorial by (TODO: @??? can't find it at the moment), really nice and well written.

Latest revision as of 09:14, 4 November 2017

This page is still a work in progress.

Preface

This tutorial is about regular expressions as they are understood by AutoIt.

About then AutoIt regular expression implementation

Before and after the PCRE implementation. StringRegExp StringRegExpReplace

Why learn and use regular expressions?

Some simple samples

Using regular classes

Creating classes

Using groups

Collection of working samples

Extracting words:

$a = StringRegExp("Some words to extract", "[\w]+", 3)
_ArrayDisplay($a, "Extracted words")

Extracting lines:

$a = StringRegExp("Some words to extract", "(.*)", 3)
_ArrayDisplay($a, "Extracted lines")

Extracting comments:

Extracting comment groups:

Extracting sentences:

Tested, verified, and working on pretty much any sentences... Needs work on words spelled with punctuation symbols, such as Mr., Mrs. Can be handled prior to regex testing by changed to mister and misses.

-JRowe

Local $a 
$a = StringRegExp($data, "(['""\w\d\(\),\;\:\-\@\&\s]+(?:.\w+)?+[\.\!\?])", 3)
_ArrayDisplay($a, "Extracted lines")

Removing html tags:

AutoIt normalize variable declarations:

Collect Global, Local and Dim statements in selected code and create declaration code to be inserted as a group in the file TODO: Not tested!

Local $a 
$a = StringRegExp($data, "\s*(Global|Local|Dim)(\s+$\w+)", 3)
_ArrayDisplay($a, "Extracted lines")

Other resources

Links to other resources or forum topics regarding the use of regular expressions in AutoIt. Forum beginners tutorial by (TODO: @??? can't find it at the moment), really nice and well written.