Sign in to follow this  
Followers 0
Dgameman1

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

3 posts in this topic

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?

 

Share this post


Link to post
Share on other sites



Figured it out

 

(?i)(.{1,30})name(.{1,30})

 

Share this post


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

 


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

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
Sign in to follow this  
Followers 0