Reputation: 1606
I am trying to get version number of my assembly. I use regex for it and here is my pattern.
$pattern = '\[assembly: AssemblyVersion\("(.*)"\)\]'
It works good but in AssemblyInfo.cs and AssemblyInfo.vb there are some special characters as example
in cs file
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.7.1.0")]
in .vb file
' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("3.2.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")>
So I want to exclude // and ' charachters in my pattern. I tried to exclude it with [^//] but it does not work. I tried something else but it did not work either.
And the second question is
in .vb file, there are different starting.
<Assembly: AssemblyVersion("3.2.0.0")>
and in c# file there are different starting
[assembly: AssemblyVersion("3.7.1.0")]
How i can include also vb version into my pattern?
Where is the problem?
Upvotes: 0
Views: 80
Reputation: 30971
You want to "exclude" rows which start either with /
or '
.
Start from setting m
(multi-line) flag in you regex.
It ensures that ^
matches start of each line (not the whole string).
Then start the regex from:
^
- acting now (in multi-line mode) as a row start marker,(?!\/|')
- a negative lookahead group with two variants
inside (do not allow that the line starts from either /
or '
),\s*
- an optional sequence of spaces.and then goes your regex.
So the whole regex should look like below:
^(?!\/|')\s*\[assembly: AssemblyVersion\("(.*)"\)\]
(remember about the m
flag).
Negative lookbehind solution mentioned in other answers has such a flaw
that even if a row starts from '
or /
but has no space thereafter,
such a regex will fail.
Upvotes: 1
Reputation: 179
You can use negative lookbehind if your library supports it
(?<!\/\/\s|\'\s)\[assembly: AssemblyVersion\("(.*)"\)\]
Edit:
For matching different brackets you can just use [variant1|variant2]
syntax
(?<!\/\/\s|\'\s)[<\[][Aa]ssembly: AssemblyVersion\("(.*)"\)[>\]]
Upvotes: 1