Mayamiko Matope
Mayamiko Matope

Reputation: 27

Detect word that end with a semicolon with regex

am tyring to write a regex pattern that detects if a word starts with var and ends with a semicolon. I have the regex code in place but when run it matches from where the last semicolon is instead of matching each word ending with a semicolon. here is my regex pattern.

dim pattern as string = "^[var].+;$"; help please.

Upvotes: 0

Views: 4885

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174786

Seems like you want something like this,

(?<!\S)var\S*;(?!\S)

(?<!\S) Asserts that the match var won't be preceded by a non-space character. \S* matches zero or more non-space characters. ;(?!\S) Matches the semicolon only if it's not followed by a non-space character.

DEMO

OR

(?<!\S)var.*?;(?!\S)

DEMO

Upvotes: 0

J&#225;n Stibila
J&#225;n Stibila

Reputation: 637

your regex now matching whole line which starts with 'v', 'a' or 'r' and ends with semicolon.

if you want match whole lines start with var and ends with semicolon this is the way:

"^var.+;$"

if only variable definitions inside line then:

"var.+;"

this second way will match following:

var a;

var b, c;

a = 5; var b, c = a; //comment

a = 5; //comment var ;

bold indicates match

Upvotes: 2

Related Questions