Jump to content

Using StringRegExpReplace to Remove all but a pattern match


Recommended Posts

I have made a function that returns a KB number from a string

#include <StringConstants.au3>
#include <Array.au3>

Local $str = 'Update for Microsoft Outlook Social Connector 2010 (KB2553308) KB234567 32-Bit Edition'

MsgBox(0,'', __UT_GetKbArticleNumberFromString($str))
MsgBox(0,'', StringRegExpReplace($str, '(?i)(KB[[:digit:]]{5,})', "")) ; trying do do it with a one liner

Func __UT_GetKbArticleNumberFromString($s_Test, $i_Flag = $STR_REGEXPARRAYGLOBALMATCH)
    Local $av_KBAN = StringRegExp($s_Test, '(?i)(KB[[:digit:]]{5,})', $i_Flag)
    If @error Then Return SetError(@error, $i_Flag, '')

    ; if an array then set @extended as the number of matches found and set the return value as the first array element
    If IsArray($av_KBAN) Then Return SetError(0, UBound($av_KBAN), $av_KBAN[0])
    Return SetError(0, 1, $av_KBAN)
EndFunc   ;==>__UT_GetKbArticleNumberFromString

I thought I would try and do it all with one line so I could just return the StringRegExpReplace but the closest I could get after an hour is a headache, and to keep everything but what I want. Is there a way to do this by replacing or should I just stick to the original idea?

Link to comment
Share on other sites

There are 2 matches in the string, so the regex must be written depending on the one you want

Local $str = 'Update for Microsoft Outlook Social Connector 2010 (KB2553308) KB234567 32-Bit Edition'

; this will get the first one
MsgBox(0,'', StringRegExpReplace($str, '(?i).*?(KB[[:digit:]]{5,}).*', "$1")) 

; this will get the last one
MsgBox(0,'', StringRegExpReplace($str, '(?i).*(KB[[:digit:]]{5,}).*', "$1"))

 

Link to comment
Share on other sites

you might want to look at a regex tester such as:  https://regex101.com/ they can really help in the beginning.  Note that it seems most everyone has a variation on any regex and I'm far from being an expert I don't take into account the number of steps or ms different expression doing the same job will take.  Good luck.

Link to comment
Share on other sites

2 minutes ago, mikell said:

There are 2 matches in the string, so the regex must be written depending on the one you want

Local $str = 'Update for Microsoft Outlook Social Connector 2010 (KB2553308) KB234567 32-Bit Edition'

; this will get the first one
MsgBox(0,'', StringRegExpReplace($str, '(?i).*?(KB[[:digit:]]{5,}).*', "$1")) 

; this will get the last one
MsgBox(0,'', StringRegExpReplace($str, '(?i).*(KB[[:digit:]]{5,}).*', "$1"))

 

Thanks mikell, my bad. I will only need the first one. There should only be one in the string so in the original function 

__UT_GetKbArticleNumberFromString

I only returned the first one found

Link to comment
Share on other sites

Thanks again Jury. I will bookmark it. I was\have used Expresso in the past, but normally just end up with msgbox and randomly picked characters from the StringRegExp part of the help file, it's my version of the infinite monkey theorem :>

Edited by benners
typo
Link to comment
Share on other sites

Another solution is to use stringregexp with flag 3, and then a for loop to combine the array back into a string. Simplifies the regex.

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

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