Reputation: 27
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
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.
OR
(?<!\S)var.*?;(?!\S)
Upvotes: 0
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