mjolnirmarkiv

Yet another one RegExp request

10 posts in this topic

#1 ·  Posted (edited)

Hello,

Have been thinking about this for a while, so decided to seek for help.

I have the following string: "...<tag>1</tag>...<tag>2</tag>...<tag>3</tag>...", which I need to test if all text within <tag> matches certain criteria, e.g. it's a number "\d+". The trick is 1) I need the entire match to fail if only one instance of the text between <tag> doesn't match "\d+"; 2) I need the string to match if there's no <tag>.

E.g.:

- "...<tag>1</tag>...<tag>2</tag>...<tag>3</tag>..." matches.
- "...<tag>a</tag>...<tag>2</tag>...<tag>3</tag>..." fails.
- "..." matches.

Thanks!

Edited by mjolnirmarkiv

Share this post


Link to post
Share on other sites



This?

Local $aTest = [ _
    "...<tag>1</tag>...<tag>2</tag>...<tag>3</tag>...", _
    "...<tag>1</tag>...<tag></tag>...<tag>3</tag>...", _
    "...<mytag>""1""</mytag>...<yourtag></yourtag>...<ourtag>3</ourtag>...", _
    "...nothing important here...", _
    "...<tag>a</tag>...<tag>2</tag>...<tag>3</tag>..." _
]

For $s In $aTest
    ConsoleWrite($s & (StringRegExp($s, '<tag>\D*</tag>') ? ' fails' : ' matches') & @LF)
Next

 

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

#3 ·  Posted (edited)

Not quite, actual criteria I need to check is ".*?(.*?\)" i.e. "text text (text text)", I've replaced it with "\d+" for the simplicity. You inverted criteria with "\D*", so I'm not sure how I can do the same with ".*?(.*?\)".

Edited by mjolnirmarkiv

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

But you gave me an idea, this works: "<tag>(?(?=[^<]+?\h\([^<]+?\))(*FAIL)|(*ACCEPT))</tag>"... unless there're easier ways to do it?

Edited by mjolnirmarkiv

Share this post


Link to post
Share on other sites

I give up aiming at targets instantly teleporting themselves thru hyperspace.


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

The dark side of the Force doesn't allow to present explicitely from the start the real requirements  :D

Share this post


Link to post
Share on other sites

Sorry :D

Here're couple of examples of matched strings:

text <‍tag>text (text)<‍/‍tag> text <tag>text (text)</tag> text.
text text text text text.

And here's failed string:

text <‍tag>text<‍/‍tag> text <tag>text (text)</tag> text.

In other words, no words in brackets within <tag> = fail.

 

But nevermind, jchd gave me an idea, so I consider this one closed. Following regexp should work: "<tag>(?![^<]+?\h\([^<]+?\))" -- I just need to invert its logic.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

You have zero width white spaces Joiners in your strings.  I dont know how much that will muck up the water, but a stringstripws may be helpful wont do anything, maybe delete all the 8205s from an ascii array or stringreplace?

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

boththose, yeah, I know, I've included "\x{200d}" in the regexp.

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

    • JohnNash
      By JohnNash
      I want to rename every new instance of notepad to notepad(random number)
      If I use WinSetTitle ( "notepad", "", "notepad("&$randomnumber&")" )
      this will work pretty good, because if more windows match the search entry it will take the newest. But what if this code runs, but there is no new instance of notepad. It will rename one that was already assigned a number. So I would like to check whether it is already renamed. For example by excluding titles that contain a ")".
      How do I do that. 
      Read this, but that is pretty confusing: http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word?rq=1
    • Mingre
      By Mingre
      #include <Array.au3> ; Script Start - Add your code below here Local $test = "<li>One<li>Inner<li>Innermost</li></li></li>" & _ "<li>Two</li> " $loob = StringRegExp($test, '\Q<li>\E(.*?)\Q</li>\E', 3) _ArrayDisplay($loob, "How to return the One... and Two?") Hello, can somebody help me:
      (1) How can I have the regexp matched the two outermost bullets? Such that:
       
      (2) How can I match the "Innermost" bullet?
      Thanks so much.
    • mLipok
      By mLipok
      #include <Array.au3> If @Compiled Then Exit Global Enum $FUNC_OUTER, $FUNC_NAME, $FUNC_PARAM, $FUNC_INNER _Example() Func _Example() Local $sIncludeDir = StringTrimRight(@AutoItExe, StringLen('AutoIt3.exe')) & 'Include\' Local $aOuterArray = _GetFunctionsToArray($sIncludeDir & 'Color.au3') If Not @error Then For $iOuter_idx = 0 To UBound($aOuterArray) - 1 _ArrayDisplay($aOuterArray[$iOuter_idx], ($aOuterArray[$iOuter_idx])[$FUNC_NAME]) Next EndIf EndFunc ;==>_Example Func _GetFunctionsToArray($sUDF_FileFullPath) Local $sUDFContent = FileRead($sUDF_FileFullPath) Local $aResult = StringRegExp($sUDFContent, '(?is)\RFunc (.*?)\((.*?)\)\v\R(.*?)\REndFunc', $STR_REGEXPARRAYGLOBALFULLMATCH) Return SetError(@error, @extended, $aResult) EndFunc ;==>_GetFunctionsToArray  
    • Digisoul
      By Digisoul
      Hello,

      I am trying to create a regExp for following HTML text:
       
      <a href="link=1">1</a> <b>2</b> <a href="link=3">3</a> | <a href="link=2">Previous</a> | <a href="link=3">Next</a> My intention is to extract href from last <a> tag.
      Here is my attempt:
      Local $reg = '(?i)\|\s?<a href="(.*?)">Next</a>' Local $text = '<a href="link=1">1</a> <b>2</b> <a href="link=3">3</a> | <a href="link=2">Previous</a> | <a href="link=3">Next</a>' $aData = StringRegExp($text,$reg,3) ConsoleWrite($aData[0]&@LF) Now problem is i am unable to extract exact href from the last <a> tag.

      Here is the output:
       
      link=2">Previous</a> | <a href="link=3 I know I can use other techniques, but i want to know, why my pattern is not working or what is the right pattern for such situation?
      Thanks in Advance.

       
    • Digisoul
      By Digisoul
      Hello,

      I want to capitalize 1st word of every sentence with StringRegExp, I am able to collect words by using this pattern:
      Local $reg = '(?:^|(?:[.!?]\s))(\w+)' now my problem is how can I exactly replace these words, e.g. In the following string:
      I only want to replace 1st "the" of the sentence with "The" but I have only words in array from RegExp, without its position in string.
      Thanks.