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

ASP.NET MVC4 #Html.ListBoxFor Multiple to DB using EF
Parsing Dynamic Data
#Html.DisplayListFor - ViewModel for Foreign Key Drop List
How to implement SelectMany in System.Linq.Dynamic ExpressionParser
Datagrid inside datagrid - With filter based on parent datagrid value
Read DER private key in C# using BouncyCastle
Acces to parent's attribute from descendant
Cannot update fields in Gridview when in “Edit” mode, asp validators prevents updating
ASP MVC PartialView is returned without layout (Razor)
Web Api and session variable?
Winform control inside HTML page
Xamarin IOS Tabbed Application Issue
C# Filtering Listview by textbox
Issue Downloading files with C#
If inside html code with entity framework
AlertDialog isn't displaying

Categories

HOME
model-view-controller
swift3
domain-driven-design
microcontroller
liquibase
cryengine
overrides
jshell
android-webview
ejb-3.0
angularjs-ng-repeat
ios-universal-links
spring-data-solr
mongoimport
angular-universal
osrm
guidewire
tracking
oxid
dat.gui
game-engine
windows-phone
aws-devicefarm
ampscript
inline-assembly
oracle-golden-gate
android-wear-2.0
oracle10g
deployment-descriptor
continuous-fourier
pydub
selenium3
contextmenustrip
wunderground
form-fields
mapquest
socketcluster
bzip2
taleo
synchronized
runtime-permissions
dynamic-linking
fckeditor
xamarin-test-cloud
selection-sort
reloaddata
esprima
geokit
instaparse
git-cherry-pick
airwatch
avi
jde
angular-i18n
boost-propertytree
innertext
markerspiderfier
clear
android-contentresolver
tvp
play-json
jain-sip
identifying
android-filterable
dcg
data-representation
sicstus-prolog
nfa
indices
shunting-yard
xcode5
conditional-comments
scjp
xpand
hotlinking
webshim
fastcgi-mono-server
mdp
mahara
mysql-error-1146
jfilechooser
cab
caroufredsel
oc4j
removeall
urlfetch
quit
m3u
hypertable
ios6-maps
event-propagation
socketasynceventargs
towerjs
recess
jexcelapi
functional-specifications
xhtmlrenderer
norton
html5-animation
p4.net
pixel-bender
html-help-workshop

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