Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for keywords

Tags:

regex

f#

How can I write a Regex to represent certain keywords without any letter/number following or preceding them? (symbols and spaces are optional)

I tried to write this, but it's doesn't seem to work:

let resWord = "[class|function|static|this|return]"
let keyword = new Regex("[^[^a-zA-Z0-9]]"+resWord+"[^[a-zA-Z0-9]$]")

I'm new to Regex, so please excuse me if it's a stupid question :)

like image 546
cookya Avatar asked Dec 20 '22 21:12

cookya


1 Answers

This can be accomplished using negative look aheads and behinds

    (?<![a-zA-Z0-9])(class|function|static|this|return)(?![a-zA-Z0-9])

or with out them.

    ([^a-zA-Z0-9])(class|function|static|this|return)([^a-zA-Z0-9])
like image 146
Uncle Iroh Avatar answered Jan 06 '23 01:01

Uncle Iroh