Regex: number or 2 numbers separated by ' - ' - asp.net

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.

Related

Check if character is number

I want to check if a character can be safely converted to a numeric by using a regex.
However, I don't see my error. Example:
stringr::str_detect("4.", pattern = "-{0,1}[0-9]+(.[0-9]+){0,1}")
This produces a TRUE. My intention was to specifiy that whenever a . follows the first sequence of numbers, there must be at least one other number, therefore (.[0-9]+){0,1}.
What's wrong here?
Note:
(.[0-9]+){0,1} is an optional pattern because {0,1} (=?) makes the .[0-9]+ pattern sequence match one or zero times. So, yes, one or more digits ([0-9]+) must follow any char other than line break chars (matched with an unescaped .), but this pattern is optional, and thus you cannot require anything with it.
. is unescaped, so it matches any char other than line break chars. Escape it to match a literal dot
Your regex is not anchored, and can match partial substrings in a longer string. Use ^ and $ to make the pattern match the whole string.
So, consider using
stringr::str_detect("4.", pattern = "^-?[0-9]+(?:\\.[0-9]+)?$")
where
^ - start of string
-? - an optional - char
[0-9]+ - one or more digits
(?:\.[0-9]+)? - a non-capturing group matching an optional sequence of a . and then 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

How to remove the first character if it's a comma using QRegExp?

I have the following QRegExpValidator
QRegExpValidator doubleValidator = new QRegExpValidator(QRegExp("[-+]?[0-9]*[\\.,]?[0-9]+([eE][-+]?[0-9]+)?"));
It's supposed to be a Double numbers validator that accepts numbers, only one "e" sign, one comma OR dot and one + or - sign at the beggining of the string or after the "e" sign. It works for every case, except that it allows the string to start with a comma or dot. I tried to use [^\\.,] and variations and they did in fact work, but in this case, it would also allow to put two +- signs.
How can I make this to work?
The [-+]?[0-9]*[.,]?[0-9]+([eE][-+]?[0-9]+)? pattern allows the , or . at the start because [-+]? and [0-9]* can match empty strings due to the ? (one or zero occurrences) and * (zero or more occurrences) quantifiers, and then [.,] matches a single occurrence of . or ,. Besides, if the method you are using does not anchor the pattern by default, you also need ^ and $ anchors around the pattern.
I suggest fixing that with
"^[-+]?[0-9]+([.,][0-9]+)?([eE][-+]?[0-9]+)?$"
^ ^^^^^^^^^^^^^^ ^
Note you do not need to escape the dot inside a character class, [.] always matches a dot char only.
The [0-9]+([.,][0-9]+)? matches 1+ digits and then an optional sequence of a . or , followed with 1+ digits.

asp net regular expression for not allowing blank spaces

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

Resources