Kyan

[SOLVED] PCRE Regular expression match but ignore string in between

23 posts in this topic

#1 ·  Posted (edited)

Hey everyone

I'm stuck with this regular expression, for matching a iso standard version

what I want to do is match the text in green: ISO 11784:1996/Amd.2:2010(E)
there is also others that look like this: ISO 11789:1999(E), and without the (E) part,

Só I did this one

 (?i)ISO.+?[\d-]+:(?:\d+/Amd.+?:)?[\d-]+(?:\(\w\))*

to match both cases but doesn't ignore the text in red

Can somebody point out what I'm doing wrong here?

 

Edited by Kyan
Solved TAG added

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites



This should work in your example cases:

(?i)(iso\s+\d+:)(.+:)?(\d{4}(?:\(e\))?)

I've put capturing parenthesis around the three groups to show what they get, but they aren't necessary.


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

Hi jchd,

It doesn't work, outputs:

Row|Col 0
[0]|ISO 11784:1996(E)
[1]|ISO 11784:
[2]|
[3]|1996(E)

on the line  [0] should have outputted: ISO 11784:2010(E) 

there's also standards with hyphens: ISO 11412-2-1:2001, that's why I used a class


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Could you simply remove the unwanted part between 2 colons ? (obviously depending on the use)

$str = "ISO 11784:1996/Amd.2:2010(E)" & @crlf & _ 
    "ISO 11789:1999(E)"

Msgbox(0,"", StringRegExpReplace($str, ':.*:', ":") )

 

Share this post


Link to post
Share on other sites

Hey mikell

Can't be done with the regular expression? I could do that, I do that to remove multiple consecutive spaces after capturing it

(?i)iso\s+\d+:(?:\d{4}.+amd.+:)?\d{4}(?:\(e\))?

This one also doesn't work, it displays 1996 -_-


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

It depends on the wanted result, simpler is often better
What is precisely the entered string ? what is precisely the needed result ?

Share this post


Link to post
Share on other sites

Already said what they are but there it goes an example

Inputs:

ISO 11784:1996/Amd.2:2010(E)
ISO 11784:1996/Amd.1:2007
ISO 11784-1-2:
1989(B)

Outputs:
ISO 11784:
2010(E)
ISO 11784:2007
ISO 11784-1-2:
1989(B)


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Hmm my previous code works well for this, doesn't it ?  :idiot:

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

With stringregexpreplace works fine, but why can't (?:) work properly?

$text="ISO 11784:1996/Amd.2:2010(E)|ISO 11784:1996/Amd.1:2007|ISO 11784-1-2:1989(B)"
$aText = StringSplit($text,"|")
For $x In $aText
    $l = StringRegExp($x,'(?i)iso\s+\d+:(?:\d{4}.+?amd.+?\:)\d{4}(?:\(\w\))?',2)
    If Not @error Then ConsoleWrite($l[0]&@LF)
Next

outputs:

ISO 11784:1996/Amd.2:2010(E)
ISO 11784:1996/Amd.1:2007

EDIT:

This one also doesn't work, is a zero width look behind

$text="ISO 11784:1996/Amd.2:2010(E)|ISO 11784:1996/Amd.1:2007|ISO 11784-1-2:1989(B)"
$aText = StringSplit($text,"|")
For $x In $aText
    $l = StringRegExp($x,'(?i)iso\s+[-\d]+:(?<=\d{4}.+?amd.+?\:)?\d{4}(?:\(\w\))?',2)
    If Not @error Then ConsoleWrite($l[0]&@LF)
Next

@Melba23 I need your expertise pls

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

You mean this ?

$text="ISO 11784:1996/Amd.2:2010(E)|ISO 11784:1996/Amd.1:2007|ISO 11784-1-2:1989(B)"
$aText = StringSplit($text,"|")
For $x In $aText
    $l = StringRegExp($x,'(?i)(iso[^:]+:)(?:\d{4}.+?amd.+?:)*(\d{4})(\(\w\))?',3)
    If Not @error Then ConsoleWrite($l[0]&$l[1]&@LF)
Next

Hard way. Removing the unwanted parts is much easier

$text="ISO 11784:1996/Amd.2:2010(E)|ISO 11784:1996/Amd.1:2007|ISO 11784-1-2:1989(B)"
$aText = StringSplit($text,"|")
For $x In $aText
    $l = StringRegExpReplace($x, '(?m):.*(?=:)|\(\w\)$', "")
    If Not @error Then ConsoleWrite($l & @LF)
Next

 

Edited by mikell
typo... sorry

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

yes, but that changes how I was capturing strings previously, I was outputting one liners with these (using StringRegExp flag 2):

BS( EN ISO)?.+?[\d-]+:[\d-]+(\(\w\))?|DIN.+?[\d-]+:[\d-]+(\(\w\))?|EN\s+?NP\s+?ISO.+?[\d-]+?:[\d-]+(\(\w\))?|EN\s+?ISO.+?[\d-]+:[\d-]+(\(\w\))?|ISO.+?[\d-]+:[\d-]+(\(\w\))?

the thing is that I found those "Amd..." that I only wanted the year preceeded by "Amd.\d:"
 

Sorry if this is kinda complicated, but it's kinda hard to understand how  a non capturing group is captured.

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites
2 minutes ago, Kyan said:

it's kinda hard to understand how  a non capturing group is captured

Kinda weird  :)

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Matching part X then non-capturing group Y then matching part Z is still matching all of XYZ. Unless you use *Replace and glue together parts X and Z by capturing them, you can't make Y vanish mysteriously. Hence my proposal : https://regex101.com/r/jezYus/1

Edited by jchd

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

My attempt

#include <Array.au3>

Global $aInput[] = ["ISO 11784:1996/Amd.2:2010(E)", _
        "ISO 11784:1996/Amd.1:2007", _
        "ISO 11784-1-2:1989(B)"]

For $i = 0 To UBound($aInput) - 1
    Local $aRegex = StringRegExp($aInput[$i], "(?i)(iso.*:).*:(.*)", 3)
    If (Not @Error) Then ConsoleWrite(_ArrayToString($aRegex, "", -1, -1, "") & @LF)
Next

I'm confused by

Quote

ISO 11784-1-2:1989(B)

In your example, the input and the output are the same for this value.

Share this post


Link to post
Share on other sites

First of all, thanks to all of you for helping me out.

@jchd wow, great website, so your proposal is to match them in groups then use StringRegExpReplace to place them correctly? Can you give me a advice how should I include other standards variants in the first capture group? There're BS EN ISO... EN ISO.., EN NP ISO...and sometimes my OCR fails and translates ISO to IS0 so I tried with this but doesn't look good: ((?:BS\h+)?(?:EN\h+)?(?:NP\h+)?IS[0o])

@InunoTaishou don't be confused, because that's a correct input-output :)


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

See the accordingly modified regexp and examples using the revised link: https://regex101.com/r/jezYus/2


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

thank you @jchd! I made it to match other standards than ISO (DIN) and the letter between parenthesis isn't always a (E), that's a revision letter, so I placed a \w instead of "e" https://regex101.com/r/jezYus/3


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Ha, yes I saw that mentionned days ago but forgot to include the variation in the regexp.

The point of importance is that you get the idea and can now fine-tune the expression to more cases, if need arises.


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

@jchd, there's some way that I start matching from the end of the document?

vlxh7dL.png


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

In case your source has a regexp-able trailer after the "right" standard reference, yes you can probably, but I'm afraid you're going to encounter too many variations there. Maybe in his case you can display the scan output and highlight the possible captures then have a user (you ?) select the correct one.

If the wanted reference is always the last one in the source text, you can always try prefixing the pattern with .* which will greedily skip everything up to the last part of the pattern.


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

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

    • ur
      By ur
      I have an exe with name "erwin Data Modeler r9.7 (32-bit)_2332.exe"
       
      THe lat 4 digits 2332 may vary and also the version number r9.7 also might vary.
      So, how to write a generic expression so that the exe can be picked from the current directory {with any version (9.7 or anything) and any build number (2332 or anything)}
       
      Thanks in Advance
    • Reizvoller
      By Reizvoller
      Howdy!
      I am trying to do something a little whacky but here's the function that's kicking back an error.
      I am fairly sure I am overlooking a simple error but I am not seeing it.
      Func mouseSpot () Global $mouseSpot = MouseGetPos() ConsoleWrite ($mouseSpot[0] & ", " & $mouseSpot[1] & @CRLF) FileWriteLine ("mouseGrabs.txt" , "MouseClick ('"'primary'"' , $mouseSpot[0] , $mouseSpot[1], 1, 10)" ) EndFunc The error displays as this 
      Any thoughts?
      Thanks!
      -Reiz
    • richtburkhart
      By richtburkhart
      I'm getting the syntax error: "Statement cannot be just an expression."
      Here's the piece of code where it occurs:
      $Check = WinExists("MySQL Installer") If $Check <> "" Then WinClose("MySQL Installer") Else $Check = ControlGetHandle("", "&No", '[CLASS:Button; INSTANCE:2]') If $Check <> "" Then ControlClick("", "&No", '[CLASS:Button; INSTANCE:2]') Sleep(10000) WinClose("MySQL Installer") EndIf EndIf Anyone know why this error is occurring?
    • SugarBall
      By SugarBall
      hey guys, anyone knows how to write a string between two other strings like for example:
      To write AbzhdfX_d between ElseIf $sVideoLink1 = "   and   " then  _IEAction ($oA, "click"), 
      Simpler: to write a between b and c ,but automatically