Jump to content
ur

Regular expression

Recommended Posts

ur

How to use regular expressions in StringSplit and StringInStr

Share this post


Link to post
Share on other sites
jchd

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
ur

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
jchd

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
Malkey

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
kylomas

@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
ur

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
ur

@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
mikell

@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
Malkey

@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

    • milkmoron
      By milkmoron
      I am trying to search in a web browser dates XX/XX/XXXX that are also links. I want to click them after and remove them from the array. This is all I have so far. Nothing shows up. What am I doing wrong?
      ControlFocus ("Customer Center", "", "")
      Local $aArray = StringRegExp('(..)/(..)/(....)', '(..)/(..)/(....)', $STR_REGEXPARRAYFULLMATCH)
      For $i = 0 To UBound($aArray) - 1
          MsgBox($MB_SYSTEMMODAL, "RegExp Test with Option 2 - " & $i, $aArray[$i])
      Next
       
    • FMS
      By FMS
      Hello,
      Does somebody knows a quick way to split eash word to an array in a text?
      I've a text and want to have each word in that text in an array.

      I tried to get in some way multiple seperators inside stringsplit but couldn't get it to work.
      I hope to get an array like :
      $aArray[0] = "PARIJS"
      $aArray[1] = "AFN"
      $aArray[2] = "Air"
      ....
      sample text: PARIJS (AFN) - Air France-KLM heeft de financieel directeur van het Franse nuts- en vervoersbedrijf Veolia, Philipe Capron, in het vizier om de nieuwe topman van de luchtvaartcombinatie te worden. Volgens de krant La Tribune wordt Capron als favoriet beschouwd om de topfunctie te gaan vervullen. AdChoices Advertentie Volgens bronnen tegen La Tribune is de keuze voor Capron zelfs al gemaakt door Air France-KLM en zal zijn voordracht volgende week worden voorgelegd aan de raad van toezicht bij het bedrijf. Capron zou dan de opvolger worden van de opgestapte topman Jean-Marc Janaillac. In mei trad Janaillac af nadat het hem niet was gelukt buiten de bonden om zaken te doen met het eigen personeel. Sindsdien heeft een interim-bestuur de dagelijkse leiding. De zestigjarige Capron is sinds 2014 financieel directeur bij Veolia. Daarvoor werkte hij onder meer bij mediaconcern Vivendi. Minder macht Overigens zijn er ook geruchten dat de nieuwe topman van Air France-KLM direct een zetel zou krijgen in de raad van commissarissen van KLM. Binnen de Nederlandse divisie, die juist alles op alles zet om minder macht naar Parijs te laten vloeien, is men volgens ingewijden verbolgen over die gang van zaken. ,,Stank voor dank", zo klinkt het in de wandelgangen. Eerder zou Janaillac ook al hebben geprobeerd om een dikkere vinger in de pap te krijgen bij KLM, dat door de bank genomen beter presteert dan haar Franse zustermaatschappij. Dit lukte eerder niet, onder meer door inmenging van het Nederlandse kabinet. AdChoices Advertentie Geruchten Een woordvoerder van Air France-KLM wilde niet reageren op de geruchtenstroom. Het concern gaat er vanuit dat binnen afzienbare tijd een nieuwe bestuursvoorzitter benoemd zal worden. Minister Cora van Nieuwenhuizen (Infrastructuur) wilde evenmin ingaan op geruchten. ,,Ik heb hierover nog geen contact gehad met KLM, dus dat wil ik eerst even afwachten'', zei ze. Tweede Kamerleden spraken deze week tijdens een debat nog maar eens hun zorg uit over de ontwikkelingen bij Air France-KLM. Zij drongen aan op herbevestiging van oude afspraken over de relatief zelfstandige positie van KLM binnen de groep.>Exit code: 0 Time: 28.52  
    • WoodGrain
      By WoodGrain
      Hi All,
      I'd like to replace 'COMMA' with ',' for example:
      $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', ',') Now I've tried escaping the ',' in various ways unsuccessfully, such as:
      '[,]'
      "[,]"
      '\,'
      [,] seems to work in the pattern, I just can't figure out how to use it in the replace, and it seems everyone online is only interested in removing/replacing commas lol.
      I also tried creating and using a variable as the replacement but also didn't work:
      $myComma = "," $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', $myComma) I'm sure it's super simple if someone could point me in the right direction - thanks.
    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i receive a strange result from splitting strings elements contained in an array .(In attachment the resulting array)
      My Array contains strings like:
       
      INFO [26.01.2018 13:53:44] [XYZ] [XYZ, XYZ] Delta pack 5.2.25_17.01.2018 server version: 5.2.25_22.01.2018 not available. My code is:
      For $a=0 to UBound($Array_info_4) -1 $Array_info_4[$a]=StringSplit($Array_info_4[$a],",",$STR_NOCOUNT) Next I tried also using a second Array to store the result, but the result was the same, like in the attachment.
      Where is my mistake? :-)
       
       
       

    • rcmaehl
      By rcmaehl
      Hi all,
      I still suck at regex as always and I need some help. According to the regex tester I normally use this should be working fine but it doesn't....
      StringRegExp($sString, "\A[1-9]+[0-9]*(\-[1-9]+[0-9]*)?,*\Z") I basically want to match:
      all numbers EXCEPT 0, but including 10, 20, etc with each number separated by a comma and allowing a "-" separated range as a value For example:
      1-5,7,10-12 I've spent a couple hours modifying it but I'm not sure where I've gone wrong. Any help would be appreciated!
×