Jump to content

Extracting full path from string with Regex


NMS
 Share

Go to solution Solved by pixelsearch,

Recommended Posts

Hello.

I've browsed the forums for a while however wasn't able to find exactly what I'm looking for.

I have the following string as example with characters marked in bold as my desired match (full file path): 
test text "" HYPERLINK "A:\test\folder\1"[Folder] more test text - HYPERLINK "A:\test\folder\1\6-07-02-2023-13-52-46-1.png"6-07-02-2023-13-52-46-1.png

This string has 2 hyperlinks with only one reffering to the actual file, while the other one points to the folder. The string can have only 1 full file path, however its placement can vary. This function is running inside the WM_NOTIFY message as I am getting the text that is currently under the mouse position so the less steps it takes to get the final link the better.

Basically what I need is always in between HYPERLINK " and .png"

$sLink = StringRegExpReplace($sText, '(HYPERLINK ")[\s\S]*?(?=png")', '$1')

The above is best what I got and even that is far from the desired result.

Link to comment
Share on other sites

Here's a simple example using StringRegExp instead of StringRegExpReplace --

#include <Array.au3>
$sText = ' test text "" HYPERLINK "A:\test\folder\1"[Folder] more test text - HYPERLINK "A:\test\folder\1\6-07-02-2023-13-52-46-1.png"6-07-02-2023-13-52-46-1.png '
$sRegExp = 'HYPERLINK "(.*?)"'
$aResults = StringRegExp($sText, $sRegExp, $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($aResults)

Edit: OIC that I missed the requirement to ignore the "folder" link. 🙁

Edited by Danp2
Link to comment
Share on other sites

RegEx is not realy needed:

$sText = ' HYPERLINK "A:\test\folder\1"[Folder] more test text - HYPERLINK "A:\test\folder\1\6-07-02-2023-13-52-46-1.png"6-07-02-2023-13-52-46-1.png '

$sbefore = '- HYPERLINK "'
$sAfter ='"6'
$iStart = StringInStr($sText, $sbefore)
$iEnd = StringInStr($sText, $sAfter)
$iCount = $iEnd - $iStart - StringLen($sbefore)
ConsoleWrite ($iStart & '|' & $iEnd & '|' & $iCount & @CRLF)
$sResult = StringMid($sText, $iStart + StringLen($sbefore), $iCount)
MsgBox(64, 'Result: ', $sResult, 15)

 

Link to comment
Share on other sites

#include <string.au3>
$sText = ' HYPERLINK "A:\test\folder\1"[Folder] more test text - HYPERLINK "A:\test\folder\1\6-07-02-2023-13-52-46-1.png"6-07-02-2023-13-52-46-1.png '
local $ext='.png'
local $_StringBetween=_StringBetween($sText,'- HYPERLINK "',$ext)
if not @error then MsgBox(64,'Result: ',$_StringBetween[0]&$ext)

 

To community goes all my regards and thanks

Link to comment
Share on other sites

While I appreciate the other responses they wouldn't work since:

$sText = ' HYPERLINK "A:\test\folder\1"[Folder] more test text - HYPERLINK "A:\test\folder\1\6-07-02-2023-13-52-46-1.png"6-07-02-2023-13-52-46-1.png '

$sbefore = '- HYPERLINK "'
$sAfter ='"6' ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This assumes it always ends at 6
$iStart = StringInStr($sText, $sbefore)
$iEnd = StringInStr($sText, $sAfter)
$iCount = $iEnd - $iStart - StringLen($sbefore)
ConsoleWrite ($iStart & '|' & $iEnd & '|' & $iCount & @CRLF)
$sResult = StringMid($sText, $iStart + StringLen($sbefore), $iCount)
MsgBox(64, 'Result: ', $sResult, 15)
#include <string.au3>
$sText = ' HYPERLINK "A:\test\folder\1"[Folder] more test text - HYPERLINK "A:\test\folder\1\6-07-02-2023-13-52-46-1.png"6-07-02-2023-13-52-46-1.png '
local $ext='.png'
local $_StringBetween=_StringBetween($sText,'- HYPERLINK "',$ext) ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< While this, starts with - HYPERLINK "
if not @error then MsgBox(64,'Result: ',$_StringBetween[0]&$ext)

 

18 hours ago, NMS said:

Basically what I need is always in between HYPERLINK " and .png"

...everything else is variable.

 

I did change a little pixelsearch's answer to suit my case better and with less steps in the end: (?is)HYPERLINK "([^"]*?\.(?:png"))

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...