How can I remove comments from CSS using Regex.Replace()?
Note - I'm not able to use the regex mentioned here in C# - Regular expression to remove CSS comments.
That would be normally enough (assuming cssLines is a string containing all lines of your CSS file):
Regex.Replace(cssLines, #"/\*.+?\*/", string.Empty, RegexOptions.Singleline)
Please note that the Singleline option will allow to match multi-line comments.
Use the regex from the linked question like so:
var rx = new Regex(#"(?<!"")\/\*.+?\*\/(?!"")");
I wonder if the following version of Maxim's solution would be faster.
"/\*[^*]*.*?\*/"
As the discussion shows this will also eliminate comments within string literals.
Very late reply but thought it will be useful for some
"(?:/*(.|[\r\n])?/)|(?:(?([^)])//.)"
This will help removing css comments both singleline and multiline.
Related
i want to change some urls to nofollow and i also want, some urls dofollow
i try to do it with this Regex :
(<a\s*(?!.*\brel=)[^>]*)(href="https?://)((?!blogs.cc)[^"]+)"([^>]*)>
i can support one url to dofollow (in this ex:"blogs.cc")
if i want to dofollow more of one, what do i do?
i try with :
(<a\s*(?!.*\brel=)[^>]*)(href="https?://)(((?!blogs.cc)[^"]+)||((?!wikipedia.org)[^"]+))"([^>]*)>
but i didn't get a correct answer
what's solution?
i resolved it and put my solution here for everybody who has same question.
just do it
(<a\s*(?!.*\brel=)[^>]*)(href="https?://)((?!(?:blogs.cc|wikipedia.org|moreUrls.com))[^"]+))"([^>]*)>
C# Sample Code:
Regex.Replace(str, "(<a\\s*(?!.*\brel=)[^>]*)(href=\"https?://)((?!(?:blogs.cc|wikipedia.org))[^\"]+)\"([^>]*)>", "<a $2$3\" $4 rel=\"nofollow\">")
i hope it would be useful
I'm trying to write a parser that will create links found in posted text that are formatted like so:
[Site Description](http://www.stackoverflow.com)
to be rendered as a standard HTML link like this:
Site Description
So far what I have is the expression listed below and will work on the example above, but if will not work if the URL has anything after the ".com". Obviously there is no single regex expression that will find every URL but would like to be able to match as many as I can.
(\[)([A-Za-z0-9 -_]*)(\])(\()((http|https|ftp)\://[A-Za-z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?)(\))
Any help would be greatly appreciated. Thanks.
Darn. It seems #Jerry and #MikeH beat me to it. My answer is best, however, as the link tags are all uppercase ;)
Find what: \[([^]]+)\]\(([^)]+)\)
Replace with: $1
http://regex101.com/r/cY7lF0
Well, you could try negated classes so you don't have to worry about the parsing of the url itself?
\[([^]]+)\]\(([^)]+)\)
And replace with:
$1
regex101 demo
Or maybe use only the beginning parts to identify a url?
\[([^]]+)\]\(((?:https?|ftp)://[^)]+)\)
The replace is the same.
Is it possible to use a Regular Expression to extract only comments from a C# file?
If so how would you do that?
Refer this: -> Finding Comments in Source Code Using Regular Expressions
after reading the article, your final RegEx would be.
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)
FOR C#
^/[/|*](.+)$ (for single line comment )
(^\/\/.*?$|\/\*.*?\*\/) (for multilne comments)
This Regex finds all the comments in a C# file.
(((?<=//).+(?=\n))|((?<=/*)[^(\*/)]+(?=\*/)))+
I have a list of links, but I need to FILTER-OUT and EXTRACT correct links from the String.
Extract should start with mywebsite.com and which end with 9-digitnumber.html
Links are strings, extracted to string
Example
http://blah.com?f=www.mywebsite.com/sdfsf/sdfsdf/sdfsdfsdf/123456789.html&sdfsdf/sf/sdfsd8sdfsdfsdf
and so on...
From this, regex must extract
mywebsite.com/sdfsf/sdfsdf/sdfsdfsdf/123456789.html
This should match the number in the end
'#"[0-9]{9}". but I am very new to regex and trying to learn how to use it properly
Parsing HTML with regexs is usually a bad idea. For you particular example, you can use:
(mywebsite.com/(.+?)\d{9})
but as Andrew said, using a regex for doing what you want is not really necessary.
/mywebsite\.com\/[a-zA-Z0-9\/]*[0-9]{9}\.html/
This question already has answers here:
Using C# regular expressions to remove HTML tags
(10 answers)
Closed 4 years ago.
Need regular expression to remove the a tag from the following url Name to output only the string "Name". I am using C#.net.
Any help is appreciated
This will do a pretty good job:
str = Regex.Replace(str, #"<a\b[^>]+>([^<]*(?:(?!</a)<[^<]*)*)</a>", "$1");
You should be looking at Html Agility Pack. RegEx works on almost all cases but it fails for some basics or broken Html. Since, the grammar of HTML is not regular, Html Agility pack still works perfectly fine in all cases.
If you are looking for just one time this particular case of anchor tag, any above RegEx would work for you, but Html Agility Pack is your long run, solid solution to strip off any Html tags.
Ref: Using C# regular expressions to remove HTML tags
You can try to use this one. It has not been tested under all conditions, but it will return the correct value from your example.
\<[^\>]+\>(.[^\<]+)</[^\>]+\>
Here's a version that will work for only tags.
\<a\s[^\>]+\>(.[^\<]+)</a\>
I tested it on the following HTML and it returned Name and Value only.
Name<label>This is a label</label> Value
Agree with Priyank that using a parser is a safer bet. If you do go the route of using a regex, consider how you want to handle edge cases. It's easy to transform the simple case you mentioned in your question. And if that is indeed the only form the markup will take, a simple regex can handle it. But if the markup is, for example, user generated or from 3rd party source, consider cases such as these:
<a>foo</a> --> foo # a bare anchor tag, with no attributes
# the regexes listed above wouldn't handle this
<b>boldness</b> --> <b>boldness</b>
# stripping out only the anchor tag
<A onClick="javascript:alert('foo')">Upper\ncase</A> --> Upper\ncase
# and obviously the regex should be case insensitive and
# apply to the entire string, not just one line at a time.
<b>bold</b>bar --> <b>bold</b>bar
# cases such as this tend to break a lot of regexes,
# if the markup in question is user generated, you're leaving
# yourself open to the risk of XSS
Following is working for me.
Regex.Replace(inputvalue, "\<[\/]*a[^\>]*\>", "")