Jump to content

Using regex to get up to 5 characters before and after match


Recommended Posts

Here is the code I've got so far

#include <array.au3>
$string = "asdasdasdasday Name is Daniel dasdsdfg dfas"
$Match = StringRegExp($string, '(?i)(.{4}?)name(.{4}?)', 1)
$Match = $Match[0] & " " & $Match[1]
ConsoleWrite($Match)

This issue with this is that if I change the 4's to 30, then I get an error because the string isn't long enough for 30 characters behind and ahead. How can I change it to be UP TO 30 characters behind and ahead?

 

Link to comment
Share on other sites

$string = "asdasdasdasday Name is Daniel dasdsdfg dfas"

$nCharsAround = 10
$sMatch = "Name"

msgbox(0, '' , stringreverse(stringreverse(stringmid($string , stringinstr($string , $sMatch) + stringlen($sMatch), $nCharsAround)) & " " & stringreverse((stringinstr($string , $sMatch) - $nCharsAround < 1) ?  (stringleft($string , stringinstr($string , $sMatch) - 1)) : (stringmid($string , StringInStr($string , $sMatch) - $nCharsAround - 1 , $nCharsAround)))))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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...