ur

Regular expression

10 posts in this topic

How to use regular expressions in StringSplit and StringInStr

Share this post


Link to post
Share on other sites



You can't. Use StringRegExp (see help there for pattern grammar) and/or StringRegExpReplace.


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

I want to check whether the particular word exists in a file and not surrounded by any double quotes.

Ex: 

b = "sunday is only holiday"

 

Here the word we are searching is only but it should return false as the word is in double quotes.

 

Can you please help me on this.

 

Share this post


Link to post
Share on other sites

Please stick to the first topic.


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

#5 ·  Posted (edited)

How to use regular expressions in StringSplit and StringInStr

Here is an example of using a  regular expression in conjunction with StringInStr.

Local $b = 'She said,"Sunday is only holiday". Once a week only.'
Local $sSearchWord = "only"

MsgBox(0, "Result", 'The word "' & $sSearchWord & '" is ' & _
        ((StringInStr(StringRegExpReplace($b, "(['""])[^\1]*\1", ""), $sSearchWord)) ? ("") : ("not")) & _ ; Ternary selection of "" or "not".
        ' present outside of quotes.')

Edit: Replaced RE pattern, "(['""])[^\1]*\1", with this one, "(['""]).*?\1",

Edited by Malkey
See Mikell post #9

Share this post


Link to post
Share on other sites

@Malkey - Thanks for the lesson in using group references. I understand it now that I've seen it in use.  I changed the ^\1 class to non-greedy to handle the case of unmatched quotes...

;Local $b = 'She said,"Sunday is only holiday". Once a week only.'
Local $b = 'She said,"Sunday is only holiday". Once a week only.  But watch out for orphan "" or '' marks.'
Local $sSearchWord = "only"

MsgBox(0, "Result", 'The word "' & $sSearchWord & '" is ' & _
        ((StringInStr(StringRegExpReplace($b, "(['""])[^\1]*?\1", ""), $sSearchWord)) ? ("") : ("not")) & _ ; Ternary selection of "" or "not".
        ' present outside of quotes.')

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Local $b = 'She said,"Sunday is only holiday". Once a week only1.  But watch out for orphan "" or '' marks.'
Local $sSearchWord = "only"

MsgBox(0, "Result", 'The word "' & $sSearchWord & '" is ' & _
        ((StringInStr(StringRegExpReplace($b, "(['""])[^\1]*?\1", ""), $sSearchWord)) ? ("") : ("not")) & _ ; Ternary selection of "" or "not".
        ' present outside of quotes.')

 

The result is different here.It is matching the word only1 also for only.

Capture.thumb.JPG.887aaad58b57ea77ab02ca

Share this post


Link to post
Share on other sites

@Malkey - Thanks for the lesson in using group references. I understand it now that I've seen it in use.  I changed the ^\1 class to non-greedy to handle the case of unmatched quotes...

;Local $b = 'She said,"Sunday is only holiday". Once a week only.'
Local $b = 'She said,"Sunday is only holiday". Once a week only.  But watch out for orphan "" or '' marks.'
Local $sSearchWord = "only"

MsgBox(0, "Result", 'The word "' & $sSearchWord & '" is ' & _
        ((StringInStr(StringRegExpReplace($b, "(['""])[^\1]*?\1", ""), $sSearchWord)) ? ("") : ("not")) & _ ; Ternary selection of "" or "not".
        ' present outside of quotes.')

kylomas

Thanks Malkey,Kylomas  and everyone who posted the reply for using regular expression in StringInStr.
I got regular expression for StringSplit also by
Melba23

https://www.autoitscript.com/forum/topic/129697-split-string-by-regular-expression/

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@Malkey
You can't use a backreference inside a character class

$str = ' aac "bbb" 111 "aac" '

$res = StringRegExpReplace($str, "(['""])([^\1]*)\1", "###")
MsgBox(0, "ex1", $res)

Edit
"Backreferences, too, cannot be used inside a character class. The \1 in a regex like (a)[\1b] is either an error or a needlessly escaped literal 1"  (Jan Goyvaerts)

Edited by mikell

Share this post


Link to post
Share on other sites

@mikell

Thanks for the heads up.

From my observations (of a backreference in a "not character" class, "[^\1]"),  there appears to be no visible error generated, and, the unnecessary escaped literal "1" is not acting as a "1".   It is acting more like a dot, "." - matching any character.   This explains why the added lazy qualifier, " [^\1]*?",  works like, ".*?".
It appears a backreference cannot be used inside a character class because is not supported at this time.

 

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

  • Similar Content

    • ISI360
      By ISI360
      Hi!

      I need a little bit help from some RegEx experts please:
      I would make my ISN AutoIt Studio faster when generating the scripttree. And what would be better to do this via regex?
      Problem is i am not really good at this regex stuff. So maybe someone could help me here.
       
      The challange is to get all Global Variables from a script via RegEx in a Array.
      Here is a example script with some tests:
      Global $Var1 = 1234 Local $Local_Var = 1234 $Ignore_me_too = 1234 Global $Var2 = 1234, $var3 = 1242 Global $ahIcons[30], $ahLabels[30] Global Const $Var4 = iniread($inivar1,"jj","jj","") , $var5= iniread($inivar2,"jj","jj","") Global $Var_String = "was" Global $Array_Test[16] = [1,15,16,0,31,15,25,15,25,30,8,30,8,15,1,15] Global Enum $MARGIN_SCRIPT_NUMBER = 0, $MARGIN_SCRIPT_ICON, $MARGIN_SCRIPT_FOLD Global Const $Delim = '\', $Delim1 = '|' Global $hard1 = "a", _ $hard2 = "b", _ $hard3 = "c"  
      The returning array should look like this:
      $Var1 $Var2 $var3 $Var4 $var5 $Var_String $Array_Test $MARGIN_SCRIPT_NUMBER $MARGIN_SCRIPT_ICON $MARGIN_SCRIPT_FOLD $Delim $Delim1 $hard1 $hard2 $hard3  
      I already made some success with a expression i found in the SciTE Jump Tool:  (\$\w+)(?:[\h\[.=+*/^,)\-])?
      This nearly returns the perfect results. But it does not check if it´s a global variable (with the const and enum options) and also returns variables in commands (for example $inivar1)
      I also found this regex: (?im:^(?=Global|Const|Enum|Static)(?:Global)?\h*(?:Const|Enum|Static)?(?:(?<=Enum)\h+Step\h+[+*-]\d+)?\h*)([^\r\n .\=]+)
      This returns also usefull results...but trying to understand this explodes my head

      Maybe someone can help me here?
      Thanks in advance!
    • TheAutomator
      By TheAutomator
      Can anyone tell me why this isn't working?..
      #include <array.au3> $regexp = StringRegExp("test 'a b c'", "'([^']|'')*'|\S+", 3) _ArrayDisplay($regexp) trying to split this "test 'a b c'  'some other '' test'' ...'" into:
      0: test
      1: 'a b c'
      2: ...
      but it gives me:
      0: test
      1: c
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.
    • tezhihi
      By tezhihi
      I have a file (see attached file) with a string all line and this problem on here is I want to separate all $00:, $03:, $10:, $20:, $25:, $30:, $40:, $45:, $110:, $115:, $120: and $T. It's mean that each $ with value start a new line ( a new paragraph). I tried with Regular Expression in notepad++ ex:
      Find ($00:, $01:, $03: and so on) with regex (\$)([0-9]+): and replace is \r\n\1\2 (I think \r\n is @CRLF (not sure :() ) Find $T with regex (\$T)(.*?)(\$T) and replace is \1\2\r\n\3 When I try these regex to replace in notepad on StringRegexReplace the results is incorrect . I have read some example simple about regex. Please advise me how to do that with some example on autoit . The result will be in attached photo. Thanks 
      ahihi.txt

    • MyEarth
      By MyEarth
      Hello, i need to validate a string can be different things. I just need a True - False return value, no groups or things like that. It will be always one line at time to be processed by StringRegEx
      Valid:
      13:52|String
      02:52 XX|String
      13:52~SUN, MON, TUE, WED, THU, FRI, SAT|String
      02:52 XX~SUN, MON, FRI|String
      22/04/2017 13:52|String
      22/04/2017 02:52 YY|String
      Not Valid
      22/04/2017 13:52~Dom|String
      I need to validate until and inclusively the | after that i don't care
      The XX and YY value are two $sVariable from my script
      SUN, MON, TUE, WED, THU, FRI, SAT are fixed value, the can be mixed but always in the same order like
      SUN
      SUN, TUE, WED
      SUN, SAT 
      The time can be 12 or 24 hours, the date is always in the same format DD/MM/YYYY. If there is a date can't be a day after that ( see not valid )
      Well i think is all
      Sorry if i don't provide a working code, regex is too way complex.
      Thanks