c#


C# Regular Expression throws Unterminated [] set


Expression:
<!--[^]+-->
Content:
Hello <!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--> world!
C# code:
Regex clearMarkup = new Regex(#"\<!--\[\^]\+-->");
clearMarkup.Replace(str, ""); // str is the content as shown above.
Expected output:
Hello world!
The Regular expression created in http://www.regextester.com/ is working properly, however, in C# it throws error as:
Unterminated [] set
I got the same error in http://regexstorm.net/tester which is .Net RegEx tester.
I tried with:
\<!--\[\^]\+--> and the issue got resolved, however, it does not match any of the contents.
Please advise, thanks.
Your problem is twofold. You need to use a RegexOption and assign the string back to str.
The RegexOptions.Singleline makes regex treat everything as if it is on one line and ignores the \n.
You also need to assign the returned string from clearMarkup.Replace to your original variable:
str = clearMarkup.Replace(str, "");
Here is a link to a working example: Example
string str = #"Hello <!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--> world!";
Regex clearMarkup = new Regex(#"(<!--\[.*\]-->)", RegexOptions.Singleline);
str = clearMarkup.Replace(str, ""); // str is the content as shown above.
str.Dump();
Brackets in a regex indicate the a character group which matches any character between the brackets.
The caret character ^ at the beginning of a character group [] has a special meaning and says "not what follows", so [^abc] matches any character EXCEPT a, b, or c.
In your expression:
[ indicates the start of a character group
^] indicates anything except a right bracket - you want to match anything EXCEPT a right bracket and there's no right bracket to match the left bracket. The ^] go together and there is no right bracket left to match the left bracket. So it complains. Maybe what you meant is this:
<--\[.*\]-->
Which matches any string that starts with
<--[
And ends with|
]-->
Without knowing what you want to match it's impossible to say.
This is a useful tutorial: http://www.regular-expressions.info/charclass.html

Related Links

DirectoryEntry.MoveTo() committing changes without calling CommitChanges()
How to use configuration settings in a Universal Windows App
Displaying an item that meets 2 conditions in a SQL database in a Webform label
How does MVC know which controller to call, when they are not annotated? [closed]
Allow Time as Hours and Minutes in a Datagridview column
DacFx: Error loading Microsoft.Data.Tools.Schema.Sql.resources
How to have a fixed line spacing in a textbox?
C# - Is it better to use is and cast or to use as and check for null? [duplicate]
The type or namespace name 'MovieTexture' could not be found.Are you missing a using directive or an assembly reference?
Deserialize XML dateTime to UTC
Is it possible to call the explicit interface implementation of the base class in c#?
Can/will a long running thread be terminated, when the app pool is still running
How to create binding property in Xamarin.Forms?
Can I set position of richtextbox?
EF filtering child collection and grandchild collection
Can't Deserialize DateTime to local time using JsonSerializer

Categories

HOME
javafx
kendo-ui
ftp
netty
blob
crm
azure-resource-manager
multipartform-data
atlassian-fisheye
minimax
email-attachments
resharper
jetbrains
lambda-calculus
tag-cloud
android-actionbar
audio-streaming
ampscript
bug-reporting
xna
ng-tags-input
datanucleus
orgchart
resx
angular-schema-form
csproj
javapns
selenium3
blockly
language-detection
spring-oauth2
unity3d-5
traitsui
mkdir
javafxports
leaderboard
apiman
intervention
piranha-cms
synchronized
text-formatting
standard-error
openidm
e4
programming-pearls
esprima
sqlproj
computed-properties
consolidation
jira-agile
qtruby
nimbus
avi
ideavim
music-player
oryx
encode
app-data
controlpanel
php-gd
jain-sip
first-order-logic
syncano
properties-file
system.diagnostics
parseexception
autopep8
numerical-recipes
backtrace
crystal-reports-7
api-eveonline
lwuit
cocos2d-x-2.x
ios-enterprise
ccmenuitem
autofilter
surfaceview
xcode5.1
google-oauth-java-client
octokit
playing-cards
qgraphicsscene
string-length
teamsystem
event-propagation
utm
discussion-board
file-processing
sipdroid
method-call
onunload
mediastreamsource
miniport
web-search
glassfish-embedded
djangoappengine
qlibrary
onresize

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App