Kyan

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

22 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

#12 ·  Posted

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

#14 ·  Posted

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

    • Reizvoller
      FileWriteLine error in expression
      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
      Syntax Error
      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
      Write string between strings?
      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
    • RaiNote
      Just A Problem don't know how to name it^^
      By RaiNote
      Well Hello,
      I currently a bit sad because i can't find anywhere in helpfiles and google away in autoit to check if a number between another number is the same as 4 variables which is checking if the main variable got the same princip of looking >.<.( I really can't explain it at all)
      Case $Send2Box $ReadText2Box = GuictrlRead($Text2Box) _GUICtrlEdit_AppendText($Console, $ReadText2Box & @CRLF) If $ReadText2Box = '/ping ' & $ip1 & '.' & $ip2 & '.' & $ip3 & '.' & $ip4 Then $IP = StringRight('/ping ', 1) $Ping = Ping($IP) _GUICtrlEdit_AppendText($Console,'Ping is: ' & $Ping & 'ms') GUICtrlSetData($Text2Box,'') EndIf Next try to explain ^-^ i want to check if the text in the inputbox($Text2Box which is read from $ReadText2Box) the same looking like a ip but with a '/ping ' before and i don't know how to make the variables to check this.. and getting after it checked that it is truly a IP to ping it and send it to $Console to display the ms
      I hope i could explained it anyway to understand it(if i write more i am getting more confused)
      Thanks to everyone who try's to help me
    • prazetto
      Computing Padding Algorithm
      By prazetto
      For some obvious reason,
      Sucessfully to translate an #computing padding algorithm,
      and outputting same value with example of an explanation
      Lets take a breath, wiki say:
      The following formulas provide the number of padding bytes required to align the start of a data structure (where mod is the modulo operator):
      # pseudo-code, see actual code below padding = (align - (offset mod align)) mod align new offset = offset + padding = offset + (align - (offset mod align)) mod align For example, the padding to add to offset 0x59d for a structure aligned to every 4 bytes is 3. The structure will then start at 0x5a0, which is a multiple of 4. Note that when offset already is a multiple of align, the second modulo in (align - (offset mod align)) mod align is required to get a padding of 0.
      Then I write AutoIt3 script.
      ConsoleWrite(@CR & '+ Computing padding' & @CR) Local $align, $offset, $padding, $new_offset $align = 4 $offset = 0x59d $padding = Mod($align - Mod($offset, $align), $align) $new_offset = $offset + $padding + Mod($offset + ($align - Mod($offset, $align)), $align) ConsoleWrite('padding: ' & $padding & ' new offset: 0x' & StringLower(Hex($new_offset, 4)) & @CR) And the output is match with example in explanation.
      + Computing padding padding: 3 new offset: 0x05a0 The question #01 are is that are correct translation? changing second line on second (=) (see below) with plus sign (+) in AutoIt3 (see above) ?
      new offset = offset + padding = offset + (align - (offset mod align)) mod align .
      Again lets take a breath, wiki say:
      If the alignment is a power of two, the modulo operation can be reduced to a bitwise boolean AND operation. The following formulas provide the new offset (where & is a bitwise AND and ~ a bitwise NOT):
      padding = align - (offset & (align - 1)) = (-offset) & (align - 1) new offset = (offset + align - 1) & ~(align - 1) Then I write AutoIt3 script.
      ConsoleWrite(@CR & '+ align 2 - Power of Two' & @CR) Local $align, $offset, $padding, $new_offset $align = 2 $offset = 0x59d $padding = $align - (BitAND($offset, ($align - 1))) + BitAND((-$offset), ($align - 1)) $new_offset = BitAND(($offset + $align - 1), BitNOT($align - 1)) ConsoleWrite('padding: ' & $padding & ' new offset: 0x' & StringLower(Hex($new_offset, 4)) & @CR) And the output is.
      + align 2 - Power of Two padding: 2 new offset: 0x059e The question #02. First line and second (=) I replace with plus (+) sign, again is that correct?
      The question #03.
      What is meaning from the word of 'The Power of Two'?
      Are 2 is The Power of Two?
      Are 4, 8, 16 is The Power of Two?
      Which are the number are is The Power of Two and which is not?
      Can someone give me an confirmation if translation that pseudo-code to AutoIt3 are correct.?
      Can someone give me an explanation what is so called 'The Power of Two'.?
      Please answer even if you think this are childish question, because I'm not from Brittain nor from American.