Jump to content

Recommended Posts

Posted (edited)

It does both look ahead and look behind but looking at your code (not testing it) you may be wrong about the \B. In PCRE (as used in AutoIt) that means match when not at a word boundary, unless that's what you wanted.

Again the problem here is you are using 2 lines to do what can be done in a single line and the primary advantage to SREs is the fact they simplify jobs like this so it should not be more complex than it has to be.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Posted (edited)

Hi GPinzone,

Yes, AutoIt uses the actual PCRE engine from the official site. The version of PCRE included in the latest v3.3.6.1 AutoIt is 8.00 AFAIK, just a bit outdated (but mostly on esoteric features). There are mainly two important features that the AutoIt PCRE doesn't support right now: callbacks and Unicode character properties and script name. Oh yes there is something else: there is no interface to the DFA engine.

Those interested with PCRE callbacks may want to find discussion on this topic in the bug track system, where interim solutions were proposed.

Unicode character properties and script names are more difficult to emulate since the Unicode support has been omited in the PCRE build in order to save space.

I hope some next release of AutoIt will correct this and offer both callbacks and full Unicode support. I believe that the ever growing number of non-english / non-latin AutoIt users will benefit from the inclusion of properties tables and I feel that this will offset the minor penalty of an increase in AutoIt size.

In the meantime, one can still compile a PCRE DLL with the right options (that's easy) and wrap things in a little UDF or plugin.

Leaving that out, PCRE can still offer you a surprising wide panel of programming features: in a pattern you can have loops with or without premature exit, tests and branch, subroutines, controlled recursion, ... In short even if you don't get a perfect Turing machine, you're not very far from one.

Back to business: the look-ahead look-behind solution will work and can be better coded in only one replace call. Indeed and following up what GeoSoft just said, when coded in two steps, then the first step will replace *abc into <b>abc regardless of the presence of a terminating asterisk. Same remark for the second call: it will blindly replace abc* by abc</b> without paying attention that there was no opnening *.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted (edited)

Thanks guys! I'm learning a lot here.

A continuation of the original question. I'm moving to jchd's code:

First off, this does not work for me for some reason:

$Raw_Code = StringRegExpReplace($Raw_Code,"\*(\w+)\*","<b>$1</b>")

It will do single word entries, but not - *This should work but doesnt*

Secondly, can I combine different things in the groups for a greater effect? What I mean is, I want to include all word AND non-word characters (\w and \W) and ONLY exclude a blank white space (^\s). Is there a way of combining these effects? I couldn't find anything conclusive in the help file. What I had (that didn't work) was:

$Raw_Code = StringRegExpReplace($Raw_Code,"\*(\w+\W+^\s)\*","<b>$1</b>")

or

 $Raw_Code = StringRegExpReplace($Raw_Code,"\*([\w+\W+^\s])\*","<b>$1</b>")

Thanks for your help!

EDIT: As I've been looking back over it, I think I see the issue. Multi-word entries require it to accept the space between the words. That might be an issue. I'll keep looking over it.

I did find that this worked well, although, I would like to exclude *'s if possible:

$Raw_Code = StringRegExpReplace($Raw_Code,"\*(\S+)\*","<b>$1</b>")

EDIT2: I got one to work!

$Raw_Code = StringRegExpReplace($Raw_Code,"\*([^*\s])(.+)([^*\s])\*","<b>$1$2$3</b>")

So, for those of you who (like me) are noobs at the SRER's, here's the explanation of my syntax:

In: "\*([^*\s])(.+)([^*\s])\*"

\* - The first asterisk (does not save as variable, as it is not in ( )

([^*\s]) - Any character that is NOT a space, or an asterisk, saved as $1

(.+) - ALL characters matched, saved as $2

([^*\s]) - Any character that is NOT a space, or an asterisk, saved as $3

\* - The trailing asterisk (does not save as variable)

So, basically, I read the asterisk, the first character, everything in between, the last character and the last asterisk.

So, the replacement code: <b>$1$2$3</b>

<b> - replaces the first asterisk

$1 - inputs the first character read with ([^*\s])

$2 - inputs all the characters read the (.+)

$3 - inputs the last character read with ([^*\s])

</b> - replaces the last asterisk

This thread has really helped me learn a lot!

-Fett

Edited by fett8802
[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Posted

@fett8802,

A forum like this one is not only helpful when it provides answers but also --and possibly is it then more helpful-- when it provides a broader vue on the issue at hand.

RegExps are typically a domain where a solution for one problem has to be adapted to another close one. That's why being verbose is not to be assimilated to pedantism -- and I still have to learn a lot on REs myself.

What you came up with is close to a working solution, but realize that it will leave *at* alone (due to the "one or more character" .+ in group $2). It will as well be inoperant on *a* for a comparable reason.

More importantly try your final pattern above with the test input string below and see that you need to stop capturing well before. This is due to group 2 capturing too much (greedy) and so capturing * as well.

Once you say (for instance) that in your case what you mean by "one or more word" is one or more of [one {non-whitespace and not *} followed by zero or more of {any character not *}], you can assemble basic building blocks to achieve that result.

\* to match the opening asterisk

( capture everything inside the asterisks

[^ *] first character which needs to be anything but space and * (this condition can be made more restrictive of course)

[^*]* the longest sequence of characters not asterisk (this condition can be made less permissive!)

) end capture group

\* to match the closing asterisk

If you try the above pattern '\*([^ *][^*]*)\*'

on input string 'this * *is* text but not all * ** *** are replaced *this is nice ,* now *this works fine... * this as well *--* that number *666* and also *1.23*!'

gives

'this * <b>is</b> text but not all * ** *** are replaced <b>this is nice ,</b> now <b>this works fine... </b> this as well <b>--</b> that number <b>666</b> and also <b>1.23</b>!'

I hope this gives you idea on how to elaborate the best (or simply a good) pattern for your actual conditions. Maybe as previously said it could bebefit to subsequent readers as well.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

I found another fun one in doing what I'm trying:

The code:

$Raw_Code = StringRegExpReplace($Raw_Code,"(\[)(.+)(\|)(.+)(\])","<a href=""$4"">$2</a>")

When this is entered:

[Hyperlink|http://website.com]

[Hyperlink|http://www.website.org]

Will give this:

<a href="http://website.com">Hyperlink</a>

<a href="http://www.website.org">Hyperlink</a>

I'm starting to really like this function.

@jchd - Were you commenting about the second edit of my post? Or is that the same thing I did? Thanks!

[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Posted

Yes, I was off most of the time this afternoon and found your post well after the second edtit stabilized. So I refer to the last part.

In your last exemple 10 min ago, you don't need to capture each character individually in ts own group. If you don't need it afterwards, just match it without grouping.

StringRegExpReplace($Raw_Code,"\[(.+)\|(.+)\]", '<a href="$2">$1</a>')

will work fine and is simpler to understand and maintain. Of course this one is easy but when you have more complex stuff, it's important to keep patterns as straightforward as possible.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

  • Moderators
Posted

fett8802,

I'm starting to really like this function

Push your chair away from the keyboard, go into the kitchen and have a beer immediately!

You are begin to show worrying symptons here - unless you have masochistic leanings you should never "like" or "enjoy" SREs. :P You might, at a pinch, feel a small glow of satisfaction when one of the little buggers works, but that is as far as it should ever go for a normally adjusted person.

If you need further warning, just look at George! :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

:x

:P

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Posted (edited)

Yes, I was off most of the time this afternoon and found your post well after the second edtit stabilized. So I refer to the last part.

But isn't this (what I posted):

So, for those of you who (like me) are noobs at the SRER's, here's the explanation of my syntax:

In: "\*([^*\s])(.+)([^*\s])\*"

\* - The first asterisk (does not save as variable, as it is not in ( )

([^*\s]) - Any character that is NOT a space, or an asterisk, saved as $1

(.+) - ALL characters matched, saved as $2

([^*\s]) - Any character that is NOT a space, or an asterisk, saved as $3

\* - The trailing asterisk (does not save as variable)

Functionally the same as this (what you posted):

\* to match the opening asterisk

( capture everything inside the asterisks

[^ *] first character which needs to be anything but space and * (this condition can be made more restrictive of course)

[^*]* the longest sequence of characters not asterisk (this condition can be made less permissive!)

) end capture group

\* to match the closing asterisk

:x

-Fett

Edited by fett8802
[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Posted (edited)

Nope, please do _try_ both patterns on my (naïve) test string.

Edit: ... and compare outputs!

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

Spoiler in plaintext: understand that metacharacters don't generally have their usual meaning inside a [] character class. Hence [^*\s] means any char not * nor s (here \s is the letter s, while in means any whitespace outside a class).

But one real difference is that "\*([^*\s])(.+)([^*\s])\*" will need at least 3 chars to match, due to use of + instead of * as repeater. Furthermore, (.+) or (.*) will match the _largest_ sequence possible that matches (greedy + or * repeater), possibly eating any * inside. The result is that you can only have one *xyz* sequence per line. Either you should code the central group as (.*?) (lazy * repeater, eating the _smallest_ sequence that still matches) or exclude the * from the allowed charset in this group.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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
×
×
  • Create New...