Jump to content
Sign in to follow this  
Astormooke

Reg Expression

Recommended Posts

Astormooke

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.

 

Share this post


Link to post
Share on other sites
somdcomputerguy

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.

Share this post


Link to post
Share on other sites
Astormooke

Ok yes I like that solution... However purely as an exercise is there anyway to produce that same output using regex?

Share this post


Link to post
Share on other sites
somdcomputerguy

I know there is, there has to be, but my RegEx knowing is weak, that's why I posted the StringBetween solution..


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

Share this post


Link to post
Share on other sites
Malkey

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)

Share this post


Link to post
Share on other sites
mikell

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

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  

×