Regular Expression to remove contents in string - asp.net

I have a string as below:
4s: and in this <em>new</em>, 5s: <em>year</em> everybody try to make our planet clean and polution free.
Replace string:
4s: and in this <em>new</em>, <em>year</em> everybody try to make our planet clean and polution free.
what i want is ,if string have two <em> tags , and if gap between these two <em> tags is of just one word and also , format of that word will be of ns: (n is any numeric value 0 to 4 char. long). then i want to remove ns: from that string. while keeping punctuation marks('?', '.' , ',',) between two <em> as it is.
also i like to add note that. input string may or may not have punctuation marks between these two <em> tags.
My regular expression as below
Regex.Replace(txtHighlight, #"</em>.(\s*)(\d*)s:(\s*).<em", "</em> <em");
Hope it is clear to my requirement.
How can I do this using regular expressions?

Not really sure what you need, but how about:
Regex.Replace(txtHighlight, #"</em>(.)\s*\d+s:\s*(.)<em", "</em>$1$2<em");

If you just want to take out the 4s 5s bit you could do something like this:
Regex.Replace(txtHighlight, #"\s\d\:", "");
This will match a space followed by a digit followed by a colon.
If that's not what you're after, my apologies. I hope it might help :)

Related

RegEx for Client-Side Validation of FileUpload

I'm trying to create a RegEx Validator that checks the file extension in the FileUpload input against a list of allowed extensions (which are user specified). The following is as far as I have got, but I'm struggling with the syntax of the backward slash (\) that appears in the file path. Obviously the below is incorrect because it just escapes the (]) which causes an error. I would be really grateful for any help here. There seems to be a lot of examples out there, but none seem to work when I try them.
[a-zA-Z_-s0-9:\]+(.pdf|.PDF)$
To include a backslash in a character class, you need to use a specific escape sequence (\b):
[a-zA-Z_\s0-9:\b]+(\.pdf|\.PDF)$
Note that this might be a bit confusing, because outside of character classes, \b represents a word boundary. I also assumed, that -s was a typo and should have represented a white space. (otherwise it shouldn't compile, I think)
EDIT: You also need to escape the dots. Otherwise they will be meta character for any character but line breaks.
another EDIT: If you actually DO want to allow hyphens in filenames, you need to put the hyphen at the end of the character class. Like this:
[a-zA-Z_\s0-9:\b-]+(\.pdf|\.PDF)$
You probably want to use something like
[a-zA-Z_0-9\s:\\-]+\.[pP][dD][fF]$
which is same as
[\w\s:\\-]+\.[pP][dD][fF]$
because \w = [a-zA-Z0-9_]
Be sure character - to put as very first or very last item in the [...] list, otherwise it has special meaning for range or characters, such as a-z.
Also \ character has to be escaped by another slash, even inside of [...].

What Quotation Marks Should I Use In CSS? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Which type of quotes we should use in css background url (“…”)? Single, double or no quote needed?
Simple question. What quotation marks should I use in CSS?
Option #1:
background: url( 'foo.png' );
Option #2:
background: url( "foo.png" );
Both works on "normal browsers". I just want to follow the standards.
The standards say:
The format of a URI value is 'url(' followed by optional white space followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional white space followed by ')'. The two quote characters must be the same.
i.e. none, single or double. If you care about IE5/Mac (which you probably don't these days) avoid ', otherwise use whatever makes you comfortable.
The standard is that you can use either for strings, and you can use either or neither for URLs. For the most part there's no real difference, but if the value you're trying to quote has double-quotes in it then it's easier to use single-quotes, and vice-versa.
Besides that, you're welcome to prefer one or the other consistently if you think it makes your files nicer and easier to edit, but nobody will shoot you for using the "wrong" one.
I use double quotes...That's how I've seen it in books and tutorials. Just be consistent in your own work

Regex: Match opening/closing chars with spaces

I'm trying to complete a regular expression that will pull out matches based on their opening and closing characters, the closest I've gotten is
^(\[\[)[a-zA-Z.-_]+(\]\])
Which will match a string such as "[[word1]]" and bring me back all the matches if there is more than one, The problem is I want it to pick up matchs where there may be a space in so for example "[[word1 word2]]", now this will work if I add a space into my pattern above however this pops up a problem that it will only get one match for my entire string so for example if I have a string
"Hi [[Title]] [[Name]] [[surname]], How are you"
then the match will be [[Title]] [[Name]] [[surname]] rather than 3 matches [[Title]], [[Name]], [[surname]]. I'm sure I'm just a char or two away in the Regex but I'm stuck, How can I make it return the 3 matches.
Thanks
You just need to make you regex non-greedy by using a ? like:
^(\[\[)[a-zA-Z.-_ ]+?(\]\])
Also there is a bug in your regex. You've included - in the char class thinking of it as a literal hyphen. But - in a char class is a meta char. So it effectively will match all char between . (period) and _ (underscore). So you need to escape it as:
^(\[\[)[a-zA-Z.\-_ ]+?(\]\])
or you can put is in some other place in the regex so that it will not have things on both sides of it as:
^(\[\[)[a-zA-Z._ -]+?(\]\])
or
^(\[\[)[-a-zA-Z._ ]+?(\]\])
You need to turn off greedy matching. See these examples for different languages:
asp.net
java
javascript
You should use +? instead of +.
The one without the question mark will try to match as much as possible, while the one with the question mark as little as possible.
Another approach would be to use [^\]] as your characters instead of [a-zA-Z.-_]. That way, a match will never extend over your closing brackets.

Don't want spaces in the text, but this regex is passing not sure why

I am using the following regex
/[a-zA-Z0-9]+/i.test(value)
If I enter a space in the word, it passes.
I don't see where spaces are aloud in the regex, why is it passing?
You need to set the beginning and end bounderies so that the entire string must match the regular expression, otherwise it'll look for any match (which in this case is one or more of the characters specified).
Try this:
/^[a-zA-Z0-9]+$/i.test(value)
Because you haven't anchored it.
For these sorts of tests, it's typically safer to make sure you don't have the negated character class:
/[^a-zA-Z0-9]/

Regular Expression to limit string length

I have an issue where I need to use a RegularExpressionValidator to limit the length of a string to 400 Characters.
My expression was .{0,400}
My question: Is there a way to limit the length of characters to 400 without taking into consideration blank spaces?
I want to be able to accept blank spaces in the string but not count it in the length. Is this possible?
I pretty much agree with Greg, but here's the regex you want:
^\s*([^\s]\s*){0,400}$
#Boopid: If you really meant only the space character, replace \s with a space in the regex.
It sounds like you might want to write your own validator class instead of using the RegularExpressionValidator. Regular expressions certainly have their uses, but this doesn't sound like one of them.
Your custom validator could remove all the spaces, then check the length of the string. Ultimately, the code will be more readable than a regular expression that does the same thing.

Resources