Jump to content

Recommended Posts

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

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

@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

    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      v1.0.1
       
      Example:
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • By BlueBandana
      Is there a way to output the regex matches into a file?
      I have a script to compare two files and check for regex matches.
      I want to output the matching regex of 'testexample.txt' to another file.
      #include <MsgBoxConstants.au3> #include <Array.au3> $Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt") $Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt") For $p = 0 To UBound($Dictionary) - 1 Step 1 $pattern = $Dictionary[$p] For $i = 0 To UBound($Read) - 1 Step 1 $regex = $Read[$i] If StringRegExp($regex, $pattern, 0) Then MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " ) Else MsgBox(0, "ResultsFail", "The string isn't in the file.") EndIf Next Next  
    • By guner7
      Hello,
      I need some help to parse the Green highlighted value with from below text:
      RESISTOR  THICK FILM 4.64K ±1% 1/4W ±100PPM/°C 1206 SMT
      RESISTOR  THICK FILM 3.83K ±1% 1/4W ±100PPM/°C 1206 SMT
      RESISTOR CARBON FILM 22K ±10% 1/2W AXIAL THT
      RESISTOR  WIREWOUND  22 ±5% 3W ±30PPM/°C AXIAL THT
      RESISTOR  METAL OXIDE 4.7K ±5% 2 W ±300PPM/°C AXIAL THT
      RESISTOR  THICK FILM 0   1/8W  0805 SMT
      I am using positive look behind.:
      (?<=FILM|WOUND|OXIDE).+ Can only pull this off:
      4.64K ±1% 1/4W ±100PPM/°C 1206 SMT 3.83K ±1% 1/4W ±100PPM/°C 1206 SMT 22K ±10% 1/2W AXIAL THT  22 ±5% 3W ±30PPM/°C AXIAL THT 4.7K ±5% 2 W ±300PPM/°C AXIAL THT 0   1/8W  0805 SMT I'm trying the \b word boundary to no avail at this point. Appreciate if anyone would guide me on this?
    • By junichironakashima
      Im creating a code that will work in this sequence:
      1. Copy the text (question) in one atea of the screen
      2. Catch the 2 strings (number)
      3. Multiply the 2 strings ( $1*$2)
      4. Click the next area to put the answer
      5. Paste the answer
       
      This is my code
       
      MouseClick($MOUSE_CLICK_LEFT, 479, 802, 3, 1) ;Clicking all of the text
      Send("^c") 
      $x = StringRegExpReplace(ClipGet(), 'What is (\d*) x (\d*) \?$', "$1*$2")
      MouseClick($MOUSE_CLICK_LEFT, 480, 844, 1, 1)
      ClipPut($x)
      Send("^v")
       
      However the output is this
      $1*$2
       
      How can I make it solve itself? Because I tried this code:
      MouseClick($MOUSE_CLICK_LEFT, 479, 802, 3, 1) ;Clicking all of the text
      Send("^c")
      MouseClick($MOUSE_CLICK_LEFT, 480, 844, 1, 1) $x = Execute(StringRegExpReplace(ClipGet(), 'What is (\d*) x (\d*) \?$', "$1*$2"))
      ClipPut($x)
      Send("^v")
      Output is just blank text

    • By gruntydatsun
      I have an XML file and every time there are three lines in a row with only <null/> in them, i want to insert a fourth line with <null/>.   Each line starts with 3 white spaces, followed by <null/> and ends with a white space followed by CR LF.   The presence of the three lines as described is unique to the points where I want to insert a line in this document.
       I'm trying to figure out how to apply the repeating part of a regex  {1,4} but apply it to this whole segment. 
      So far I have the below which picks up an individual line ok:
      ^\s{3}<null/>\s\r\n I tried wrapping it all in braces () then adding {3} but I'm obviously getting something wrong. 
      Attached is a section from the xml file with a block of nulls that should be matched if anyone would like to have a look.
      Help_From_Forum.xml
×
×
  • Create New...