Regex extract string based on pattern - asp.net

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

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.

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.

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

Regex for matching URL with no extension and with specific word in it

I'm receiving a URL like this: http://blog.test.com/post/2017/1/testpost-blog-January
I want to make a Regex pattern that matches an URL with no extension and with the word post in it.
[Examples]
http://blog.test.com/post/2017/1/testpost-blog-January -> match
http://blog.test.com/post/2017/1/testpost-blog-January.aspx -> no match
http://blog.test.com/Testpage.aspx -> no match
What I've tried so far:
(?=.*^([^.]+)$)(?=.*post)
This is not working somehow.
Do you have any idea how to rewrite my pattern correctly?
Thx :)
You need to fix the lookaheads like this:
^(?=.*post)(?!.*\.[^\/.]+$).*
See the regex demo (in the demo, \n is added to the negated character class since it is a multiline demo.)
Details:
^ - start of string
(?=.*post) - a positive lookahead requiring the string to have a post literal char sequence inside after any 0+ chars
(?!.*\.[^\/.]+$) - a negative lookahead that will fail the match if after any 0+ chars there is a dot followed with 1+ chars other than / and . up to the end of string
.* - matching pattern getting the whole line.

Resources