I have a MVC3 model and a textbox that accepts some html it works perfectly, but how can I filter the 2 words www and porn as there are some people that are constantly writing that,my model is
[AllowHtml]
[RegularExpression(#"^([^<]|<em>|</em>|<u>|</u>||a z|A Z|1 9|)*$", ErrorMessage = "Invalid character")]
public string mytextbox { get; set; }
So that above works correctly user's can put in HTML only using em and u . As stated above how can i filter the worlds www and porn . I been looking everywhere but haven't found how to do it..
You could use a negative lookahead, like:
^(?!.*(?:www|porn))(?:[^<>]|<em>|</em>|<u>|</u>)*\z
The rest of your expression was useless, so I removed it. Note that you have to get the encoding right when doing filtering like this. And there could be other ways around it.
It will also match any words which include www or porn. If you don't want that you could add some \bs.
Related
I want to filter a string from the URLs in Google Analytics. This can be done using the Views > Filter > Exclude using RegEx, but I have been unable to get it to work.
An outline of how these filters are set up, can be found here, however, I can not work out how to isolate the string using RegEx. I believe it will need to be one filter per URL type.
The URLs follow this format:
/software/11F372288FA/pagename
/software/13F412C5FA/pagename/summary
/software/XIL1P0BFXCKM81/pagename2
I need to exclude this part of the URL:
/11F372288FA/
So that the URL data (e.g. Session time) is recorded against:
/software/pagename
/software/pagename/summary
/software/pagename2
I have worked out that I can isolate the string using thing following RegEx
^\/validate\/(..........)\/accounts\/summary$
It is not very elegant and would require a filter for every URL type.
Thanks for the help!
I'm not certain if this will work in your exact case but instead of using regex for this it might be easier to just create a new string from the start to the end of "software" and append everything from pagename to the end. In Java this might look something like:
String newString = oldString.substring(0, 9) + oldString.substring(oldString.indexOf("pagename"));
Take note though that this will only work if the "software" at the start is always the same length and you are actually only excluding things between "software" and "pagename".
Supposing that I have the following URL as a String;
String urlSource = 'https://www.wikipedia.org/';
I want to extract the main page name from this url String; 'wikipedia', removing https:// , www , .com , .org part from the url.
What is the best way to extract this? In case of RegExp, what regular expression do I have to use?
You do not need to make use of RegExp in this case.
Dart has a premade class for parsing URLs:
Uri
What you want to achieve is quite simple using that API:
final urlSource = 'https://www.wikipedia.org/';
final uri = Uri.parse(urlSource);
uri.host; // www.wikipedia.org
The Uri.host property will give you www.wikipedia.org. From there, you should easily be able to extract wikipedia.
Uri.host will also remove the whole path, i.e. anything after the / after the host.
Extracting the second-level domain
If you want to get the second-level domain, i.e. wikipedia from the host, you could just do uri.host.split('.')[uri.host.split('.').length - 2].
However, note that this is not fail-safe because you might have subdomains or not (e.g. www) and the top-level domain might also be made up of multiple parts. For example, co.uk uses co as the second-level domain.
For example, if a value matches "an email pattern", then remove the key also associated to it from the url.
If url is
https://stackoverflow.com/?key1=test&key2=test#gmail.com&key3=something&key4=another#email.com,
then remove key2=test#gmail.com and key4=another#email.com, so that the new url will be
https://stackoverflow.com/?key1=test&key3=something
Here, key names are not fixed and they can be anything. and also the position of the keys is not fixed.
So, want a regex to get the entire string which does not contain those key value pairs. I tried to generate the regex to match the unwanted key value pairs, but could get the rest of the string which does not match the regex.
I did it using a java program. But looking at a regex so that I can apply in the xml and avoid a java program
This is mainly to use in urlrewritefilter (tuckey) and want to remove certain query strings matching a regex.
Here is a simple solution in java (I saw your question is tagged as java). This is basically pattern that matches ? or & followed by a word then a = and then an email. You can substitute that part [.\w]+#[\w]+\.\w+ with a better email regex. Finding email with regex can be tricky with stranger emails but this would be the basic idea.
public class HelloWorld{
public static void main(String []args){
String url="https://stackoverflow.com/?key1=test&key2=test#gmail.com&key3=something&key4=another#email.com";
System.out.println(url.replaceAll("[?&]\\w+=[.\\w]+#[\\w]+\\.\\w+",""));
}
}
I have a model class with the following property for my country list.
[Required(ErrorMessage = "Please select a country")]
[RegularExpression("^(?!----------------$)", ErrorMessage= "Please select a country")]
public string Country { get; set; }
At the moment when users click on any countries they will get the error message !?
I only need to display the error message when users click on ---------------- option only.
I have already tried couple of other expressions but they seem not work at all.
also tried this : [RegularExpression("/^(?!----------------)$/"
any ideas ?
Use a pattern that matches what is valid of course. Also, do not use opening and closing forward slashes in your pattern.
An easy fix for the pattern you tried is to supplement the negative lookahead with .+:
^(?!----------------).+$
Here is a regex fiddle for the tweaked version of your pattern.
However, I would consider a pattern like the following judging by the countries in your list - and refine it as needed (e.g. for Unicode characters etcetera):
^[A-Z][a-z]+( [A-Z][a-z]+)*( \([A-Z]+\))*$
Here is a regex fiddle for this alternate (starting-point) pattern.
In my JSP I have receive some data which is coming from database my data is for example something like this :
Google is the greatest search engine ever http://www.google.com
what I wanna do is so simple: I want to make this link wrap in anchor tag using JSTL something like:
Google is the greatest search engine ever http://www.google.com
that's all !
take note that the urls are not constant, I mean I'm not sure what that be exactly & I just mentioned google here for the example.
Follow this SO question to create a replaceAll function for JSTL and then use the following pattern to replace the url to html link:
String pattern = "(http:[A-z0-9./~%]+)";
String str = "Google is the http://www.test.com greatest search engine ever http://www.google.com";
String replaced = str.replaceAll(pattern, "<a href='$1'>$1</a>");