Sign in to follow this  
Followers 0
Kyan

Regex question (dummy groups)

8 posts in this topic

#1 ·  Posted (edited)

 Hi

There's anyway to capture something in regex thats is not matched? (? :) will only match and not get captured, I want the oposit, be captured and do not matched

example:

/^(http.+/).+?(/myfolder-.+?/).+$(Something I want to be addressed and will spare a bunch of if then lines)/
Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites



PCRE (AutoIt regexp engine) forcibly matches what it captures. I don't know any regex engine working otherwise.

Nonetheless you can still StringRegExpReplace what you don't want and replace it by the empty string, leaving out what you do want.

An explicit example would help us help you.


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)

Share this post


Link to post
Share on other sites

PCRE (AutoIt regexp engine) forcibly matches what it captures. I don't know any regex engine working otherwise.

Nonetheless you can still StringRegExpReplace what you don't want and replace it by the empty string, leaving out what you do want.

An explicit example would help us help you.

I want to match a couple of URL's but for 1 domain I need to add some words, I found this kind of group "(?|)" may come handy to narrow down the output

Comments can't be captured right? since it will return nothing to be matched ((#test))

#include <Array.au3>
$url = "http://www.autoitscript.com/forum/topic/165114-regex-question-dummy-groups/#entry1205349"
$ex = StringRegExp($url,'(?i)^(http://.+?autoit.+)/.+(/topic/\d+).+?((#test))',1)
_ArrayDisplay($ex)
Exit

:/


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Outside a comment group (?#...) the # is a normal character

#include <Array.au3>
$url = "http://www.autoitscript.com/forum/topic/165114-regex-question-dummy-groups/#entry1205349"
$ex = StringRegExp($url,'(?i)(http://.+?autoit.+)/.+(/topic/\d+).+?(#.*)',1)
_ArrayDisplay($ex)

Share this post


Link to post
Share on other sites
I meant "((?#Some word I want to be capture without matching it))", wasn't talking about the "#" in the url:)
 
Why k doesn't act like in this page http://www.regular-expressions.info/keep.html ?
example:
$url = "http://www.motherinlaw.mygf.com/something"
$ex = StringRegExp($url,'(?i)^(http://www.motherinlaw\k.my.+)/',1)
 
should output ".mygf.com"

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

You seem to confuse k and K.

Based on your example, you can use "(?i)^http://www.motherinlaw.K(.+)/"

Why do you put the "http://www.motherinlaw" in the capturing group? But then notice that K is useless as the pattern "(?i)^http://www.motherinlaw.(.+)/" works equally well.

Also beware that dot (.) may not work like a literal dot (.)

1 person likes this

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)

Share this post


Link to post
Share on other sites

Another example for your consideration.

#include <Array.au3>

$url = "http://www.motherinlaw.mygf.com/something"
$ex = StringRegExp($url, '(?i)^.+\K\.my[^/]+', 1) ; Start capturing at ".my" and continue capturing sequencially all non forward slash characters.
_ArrayDisplay($ex)

$ex = StringRegExpReplace($url, '(?i)^(http://www.motherinlaw)|(/.*)$', "") ; Matches characters at the beginning and/or end of the string and replace
;                                                                    with nothing, leaving the non-matched characters in the middle of the string.
ConsoleWrite($ex & @LF)

;Both return
;.mygf.com

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

You seem to confuse k and K.

Based on your example, you can use "(?i)^http://www.motherinlaw.K(.+)/"

Why do you put the "http://www.motherinlaw" in the capturing group? But then notice that K is useless as the pattern "(?i)^http://www.motherinlaw.(.+)/" works equally well.

Also beware that dot (.) may not work like a literal dot (.)

Indeed :)

I always forget to escape dots but works anyways :), in PHP what "?" mean?  Is a "" that may or not appear?

EDIT:

 

 

Another example for your consideration.

#include <Array.au3>

$url = "http://www.motherinlaw.mygf.com/something"

$ex = StringRegExp($url, '(?i)^.+K.my[^/]+', 1) ; Start capturing at ".my" and continue capturing sequencially all non forward slash characters.

_ArrayDisplay($ex)

$ex = StringRegExpReplace($url, '(?i)^(http://www.motherinlaw)|(/.*), "") ; Matches characters at the beginning and/or end of the string and replace

;                                                                    with nothing, leaving the non-matched characters in the middle of the string.

ConsoleWrite($ex & @LF)

;Both return

;.mygf.com

 

 

So in regexR I just need to match what I don't want to replace it with nothing, nice :D

Thank you both, now I'm going to put it to work :D

 

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
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
Sign in to follow this  
Followers 0