asp net regular expression for not allowing blank spaces - asp.net

I want my validator to not allowing the user to enter more then 1 blank space since my application will crash. And a maximum of 25 characters, what is the regex for this?
<asp:RegularExpressionValidator ValidationGroup="grpSearch" ID="valSearch" ControlToValidate="txtSearchFor" ValidationExpression="^[a-zA-Z0-9][a-zA-Z0-9 ]+$" runat="server" ForeColor="Red"/>
The expression I have tried there does not work.

You may use
ValidationExpression="^(?!.{26})[a-zA-Z0-9]+( [a-zA-Z0-9]*)?$"
See the regex demo.
Details:
^ - start of string
(?!.{26}) - no 26 chars allowed (25 and fewer only)
[a-zA-Z0-9]+ - 1+ alphanumeric chars
( [a-zA-Z0-9]*)? - optional group matching a space and 0+ alphanumeric chars
$ - end of string
Just in case it is of interest: to disallow the space at the end of string, the * quantifier (zero or more occurrences) should be replaced with the + quantifier (one or more occurrences).

Try this:
^[^ ]+ [^ ]+$
This is of a similar theme, but asserts your requirement for alpha-numeric characters only:
^(A-Za-z0-9|[^ ])+ (A-Za-z0-9|[^ ])+$
Tested here: https://regex101.com/r/3w6B6h/3

Related

Regex: number or 2 numbers separated by ' - '

Can someone help me with a regular expression? I'd paste mine here but Stackoverflow doesn't seem to allow it so here's a screenshot:
It must match any number or any 2 numbers separated by a '-' and also only the first match.
You can use
^\d+(?:\.\d+)?(?:\s*-\s*\d+(?:\.\d+)?)?$
See a regex demo.
Details:
^ - start of string
\d+(?:\.\d+)? - one or more digits and an optional sequence of a . and one or more digits
(?:\s*-\s*\d+(?:\.\d+)?)? - an optional sequence of
\s*-\s* - a hyphen enclosed with zero or more whitespaces
\d+(?:\.\d+)? - one or more digits and an optional sequence of a . and one or more digits
$ - end of string.

Remove number between end of row and new space

I'm trying to remove the numbers at the beginning of a row inside quotation marks.
> g<-"My name is Paul.\nI like playing football.\n\"55012\" And that's all."
> cat(g)
My name is Paul.
I like playing football.
"55012" And that's all.
> gsub("[\r\n]\"+[[:digit:]][^[[:space:]]]*"," ",g)
[1] "My name is Paul.\nI like playing football. 012\" And that's all."
This should work, but I don't know why only \n"55 is being replaced and not the entire number.
You closed the bracket expression with a couple of redundant [...]. [^[[:space:]]] is a sequence of [^[[:space:]] and ] patterns and matches any char other than [ and whitespace and then a ] char.
However, even that is not enough to fully fix the issue.
You may use
gsub("(^|\n)\"+[0-9]+\"+\\s*","\\1", g)
See the R demo
Pattern details
(^|\n) - start of string or a newline captured in Group 1 (referred to with \1 from the replacement pattern)
\"+ - one or more double quotes
[0-9]+ - 1+ digits
\"+ - one or more double quotes
\s* - 0+ whitespaces.
See the regex demo

Regex extract string based on pattern

See below string we have in document
32Main Section
32.1Assignment and transfers by Obligors
32.2Additional Borrowers
(a)Subject to compliance with the provisions of paragraphs (c) and (d) of Clause 28.10 ("Know your customer" checks),
Output Expected (List string only if they have this xx.xx pattern at the start of the line)
32.1Assignment and transfers by Obligors
32.2Additional Borrowers
Regex we are trying \d+(\.\d{1,2}.*)
But this gives us 3rd line also which we dont have as it has number in the middle of line. We want to list only lines which start with the number or decimal...
You want to match any string starting with a digit, so ^\d+(?:\.\d{1,2})?.* that can be shortened to ^\d.* will do the job.
A better idea is to specify a delimiter for the number. Say,
^\d+(?:\.\d{1,2})?[ .].*
^^^^
Or make sure there is no digit:
^\d+(?:\.\d{1,2})?(?!\d).*
^^^^^^
The [ .] will require a space or . after the initial number.
Details
^ - start of a string
\d+ - 1+ digits
(?:\.\d{1,2})? - an optional non-capturing group
\. - a dot
\d{1,2} - 1 or 2 digits
[ .] - a space or .
.* - the rest of the string.
You can try this mate
^\d+\.\d+.*
Explanation
^ - Anchor to start of string.
\d+\.\d+ - Matches xx.xx pattern (x is number).
.* - Matches anything except newline. zero or more time (greedy mode)
Demo

regex to allow alphanumeric characters on both sides of an equal sign?

I need a regular expression to validate whether text entered in an asp.net textbox has the following format
A-za-z123456789 /s = /s A-za-z123456789
Regular expression explained:
one or more alphanumeric characters
followed by any number of spaces
an equal sign
followed by any number of spaces
one or more alphanumeric characters
[a-zA-Z0-9]*\s*\=\s*[a-zA-Z0-9]*
Replace * with + if you want one or more rather than "any" (which includes zero)
Considering your answer to the comment about requiring one or more alphanumeric characters each side:
[a-zA-Z0-9]+\s*\=\s*[a-zA-Z0-9]+
This version will only match if there is at least one alphanumeric character each side of the "=".
If zero valid
"^[a-zA-Z\\d]+\\s*=\\s*[a-zA-Z\\d]+$"
If zero not valid
"^[a-zA-Z1-9]+\\s*=\\s*[a-zA-Z1-9]+$"

Telephone number regex with optional "+"

How would I go about building a regex that allows only digits, with no spaces, and an optional "+" at the beginning?
try this
^\+?\d+$
^ anchors it to the start of the string, $ to the end
\+? is the optional +
\d is a digit and the following + is the quantifier that says at least one (digit).
A useful resource to learn regular expressions is the tutorial of regular-expressions.info
And Regexr is a very useful resource to test regular expressions, see this regex here online
This one should work: ^\+?\d+$
You need to match a +,maybe, followed by digits. The + is a special character, so you need to escape it. To match a telephone number on its own (nothing else in the string) do ^\+?\d+$, to match it in a larger string omit the ^ and $ for just \+?\d+. You can obviously also change \d+ to \d{7} if you know how many digits there should be.
I'm using the following:
(^\+?[0-9]{10,15})$
The + in the beginning is optional as indicated above, with added length restrictions (being minimum 10 digits & maximum 15)

Resources