I have a textbox and have to use the regular expressions in asp.net
My text should not allow the spaces in first and last place.
Output should be:
Valid:
[India Bangalore]
Not valid:
[ India Bangalore ]
i.e : user can enter the spaces in between the words but not in first position and last position.
If you have solution in JavaScript that is also fine.
Trim() should remove any trailing or leading spaces.
Try this please :
^[^\s].*[^\s]$
It simply match input which:
not to start with any white space ^[^\s] followed by any character even white spaces .* and not end with any white space [^\s]$.
Any way calling Trim() method on input string in server-side is much easy .
Related
I have a question similar to this one but instead of having two specific characters to look between, I want to get the text between a space and a specific character. In my example, I have this string:
myString <- "This is my string I scraped from the web. I want to remove all instances of a picture. picture-file.jpg. The text continues here. picture-file2.jpg"
but if I were to do something like this: str_remove_all(myString, " .*jpg) I end up with
[1] "This"
I know that what's happening is R is finding the first instance of a space and removing everything between that space and ".jpg" but I want it to be the first space immediately before ".jpg". My final result I hope for looks like this:
[1] "This is my string I scraped from the web. I want to remove all instances of a picture. the text continues here.
NOTE: I know that a solution may arise which does what I want, but ends up putting two periods next to each other. I do not mind a solution like that because later in my analysis I am removing punctuation.
You can use
str_remove_all(myString, "\\S*\\.jpg")
Or, if you also want to remove optional whitespace before the "word":
str_remove_all(myString, "\\s*\\S*\\.jpg")
Details:
\s* - zero or more whitespaces
\S* - zero or more non-whitespaces
\.jpg - .jpg substring.
To make it case insensitive, add (?i) at the pattern part: "(?i)\\s*\\S*\\.jpg".
If you need to make sure there is no word char after jpg, add a word boundary: "(?i)\\s*\\S*\\.jpg\\b"
I use Asp.net and C#.
I need force the User to add in a TextBox Control only between 4 and 128 characters text.
I would like to use a ValidationExpression Property for a Validation Control.
Could you point me out a correct Regular Expression?
Notes: I'm using this code right now, but it seems not working properly if there are double spaces or break line in the TextBox
ValidationExpression="^.{4,128}$"
Thanks for your time on this!
Your expression is correct. Just use the Singleline modifier, to make the dot also match newline characters.
RegexOptions.Singleline
Or as inline modifier
"^(?s)(.){4,128}$"
RegexOptions Enumeration
Regular Expression Options
The full stop or period character (.) is known as dot. It is a wildcard that will match any character except a new line (\n).
Reference: http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx
Try this instead:
ValidationExpression = "^(.|\n|\t){4,128}$"
I added tabs (\t) as well.
Tell me if it worked or not!
try this ValidationExpression = ^(\w*)(\s*)(.*){4,128}$" it will cover periods and spaces as well.
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 :)
In my requirement a Textbox should allow Alphabets,Numeric s, Special Characters,Special Symbols With at least one Alphabet.
I will try like this but i am not getting.
^\d*[a-zA-Z][a-zA-Z0-9#*,$._&% -!><^#]*$
You may want to have 2 regular expression validators; one for validating the allowed characters, and one for validating that at least on alphabet has been provided. You may be able to get at least one, but this way, you can have two separate validation messages to show the user explaining why the input is wrong.
Just match for special characters until you encounter a letter, then match for everything until the end of the string:
^[0-9#*,$._&% -!><^#]*[a-zA-Z0-9#*,$._&% -!><^#]*$
Use lookaheads :
/^(?=.*[a-zA-Z])[\w#*,$.&%!><^#-]*$/
Edit :
I assume the - is meant as the actual - character and not a range of space to !.
I removed the space character. You can of course add it if you want.
[ -!]
Effectively means :
[ -!] # Match a single character in the range between “ ” and “!”
And I have no idea what that range entails!
I have an ASP.NET website with a regular expression validator text box.
I have changed the expression in the regular expression validation property "validator expression" and after compiling (rebuild) and running, the validation CHANGEs are not reflecting.
The previous validation is working fine but the changed validation is not working.
Please help me!
edit:
First code:
([a-zA-Z0-9_-.]+)\#((base.co.uk)|(base.com)|(group.com))
Second code:
#"([a-zA-Z0-9_\-.]+)#((base\.co\.uk)|(base\.com)|(group\.com)|(arg\.co\.uk)|(arggroup\.com))"
Assuming your "first code" is a literal regex, you need to escape the hyphen in the character class with a backslash.
Your "second code" is a regex formatted as a C# verbatim string that will match an email address such as whatever#base.co.uk just fine. There is nothing wrong with this regex.
You'll have to post the code in which you are using this regex if it doesn't work the way you want.
In this part: [a-zA-Z0-9_\-.] you are escaping the hyphen. The proper way to put a hyphen in a regex character class is in first position (or it thinks it is part of a range):
[-a-zA-Z0-9_.]
Then you removed the backslash from before the #. In Perl the # would be taken as part of a list name, but in C# I am not sure what effect it would have to not escape it.
The escaping of the periods is also suspect. You might need to double them up: e.g. \\. Instead, what I would do for a period is use a character class: i.e. [.] Inside the character class the period loses its special meaning.
Try this:
#"([-a-zA-Z0-9_.]+)\#((base[.]co[.]uk)|(base[.]com)|(group[.]com)|(arg[.]co[.]uk)|(arggroup[.]com))"