Jump to content

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


Kyan
 Share

Recommended Posts

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

Link to comment
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)

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
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.

Link to comment
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

Link to comment
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)

Link to comment
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

Link to comment
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)

Link to comment
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)

Link to comment
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
 Share

×
×
  • Create New...