Jump to content
mjolnirmarkiv

Yet another one RegExp request

Recommended Posts

mjolnirmarkiv

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
jchd

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

 

  • Like 1

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
mjolnirmarkiv

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
mjolnirmarkiv

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
jchd

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
mikell

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
mjolnirmarkiv

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
iamtheky

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
mjolnirmarkiv

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

    • mLipok
      By mLipok
      In April 5, 2013 I ask @Lazycat 
      he answer:
      Then I change this tool a little.
      Now I back to this and make bigger changed.
      Here is new version.
      Update History: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/07 v3.0 * Changed: AU3Check compilant - mLipok * Changed: almost all Variables renamed - mLipok * Added: "Delete RegExp Results" - mLipok * Added: support for dual monitor - mLipok * Added: "full screen mode" - mLipok  
    • MrCheese
      By MrCheese
      argh, pulling my hair out.
      considering this post: 
       
      say for a string = "03a", how can I strip out the leading 0 and the a.
      I have tried:
      $new = StringRegExpReplace($string, '[^1-9][^0-9]', '')
       
      and various combinations:
      ^0+[^0-9]
      [^[:digit:]]
      "[^0].*"
      "^0*(d+)"
       
      I'm going loopy!
       
       
    • PClough
      By PClough
      Hi everyone!
      After updating autoit, I tried to run an old program using complex regexp's.  It did not work.  Eventually I broke the problem down to this example:
       
      #include <Array.au3> $buf = "First title" & @CRLF & "Tom" & Chr(0x92) & "s sleepwalking" & @CRLF & "Last | line" & @CRLF $items = StringRegExp($buf, '([\x20-\xff]+)\x0d\x0a', 3) _ArrayDisplay($items,'') And this is the result I get when running it:
      Row 0
       
    • Miliardsto
      By Miliardsto
      Hello . How to do that
      $regexp = starts from "abcdef" and after this could be anything in name
      WinActivate($regexp)
×