Jump to content

Reg Expression


Go to solution Solved by Malkey,

Recommended Posts

Posted

Hello,

I am having some troubles understanding why my Regexp for the following string isnt working

Atmosphere byBilly the kid onRadio Radio

now my Regexp is: "by(w){1,30} on" without the ""

I would like to understand Regexp a lot more ive read the help file and tried reading some articles off of google but still cant quite grasp the concept. I amd trying to get "Billy the kid" as the return value. Any help would be appreciated, Thanks in advance.

 

Posted (edited)

You could use the _StringBetween and StringStripWS functions.

#include <String.au3>

Local $sVar = "Atmosphere byBilly the kid onRadio Radio", _
      $aArr = _StringBetween($sVar, "by", "on")

ConsoleWrite(StringStripWS($aArr[0], 8) & @LF)
edit: I added the StringStripWS function.. Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

  • Solution
Posted

Here is an example.

#include <Array.au3>

Local $sString = "Atmosphere byBilly the kid onRadio Radio"

;Capture all word and horizontal whitespace characters between "by" and " on".
Local $aArray = StringRegExp($sString, "by([\h\w]+?) on", 3)
_ArrayDisplay($aArray)

;Or

; Replace from beginning of string to "by" and/or Replace from " on" to end of string with nothing,"".
Local $sSubString = StringRegExpReplace($sString, "(^.*by)|( on.*$)", "")
MsgBox(0, "Results", $sSubString)
Posted (edited)

An other one (variant of Malkey's #2)

Local $sString = "Atmosphere byBilly the kid onRadio Radio"

; replace the whole string by the part between 'by' and ' on' using the backreference $1
Local $sSubString = StringRegExpReplace($sString, "^.*by(.*?) on.*$", "$1")
MsgBox(0, "Results", $sSubString)
Edited by mikell

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
×
×
  • Create New...