Valid regex expressions won't work with AngularJS - angularjs

I'd like to check if user input is correct for phone numbers in two formats:
01 1234567 (two numbers, a space, seven numbers)
+12 123 123 123 123 (plus sign, two numbers, a space, three numbers, a space, three numbers, a space, three numbers
no character at all (no input)
I wrote a regex for this [0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3}|[0-9]{2} [0-9]{7}|. It works when checked with online regex checkers, but it won't work (user can write whatever they want) when used with AngularJS: ng-pattern="[0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3}|[0-9]{2} [0-9]{7}|".

You need to define a regex that will match the whole string that matches your patterns as optional patterns:
ng-pattern="/^(?:\+[0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3}|[0-9]{2} [0-9]{7})?$/"
^^ ^^
Or, a bit shorter:
ng-pattern="/^(?:\+[0-9]{2}(?: [0-9]{3}){3}|[0-9]{2} [0-9]{7})?$/"
If you define the pattern in a JS file as a variable use
var mypattern = /^(?:\+[0-9]{2}(?: [0-9]{3}){3}|[0-9]{2} [0-9]{7})?$/;
Note that when using regex delimiters the anchors are required for the regex to match entire input.
See the regex demo.
Details
^ - start of string
(?: - start of an optional non-capturing group:
\+ - a + char
[0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3} (equal to [0-9]{2}(?: [0-9]{3}){3}) - 2 digits and then 3 occurrences of a space, 3 digits
| - or
[0-9]{2} [0-9]{7} - 2 digits, space, 7 digits
)? - end of the optional group
$ - end of string.

Related

Is there a Regex to trim numbers after two decimal places?

I am trying to reduce the number of decimals and for some reason, using a "fixed decimal" 258.2 does not change all numbers in my column to two decimal places.
Pretty much I have the following after specifying the number as a fixed decimal with 2 places:
6.933141
5.13
1.56
2.94
1.54
6.470931
So changing the amount of fixed decimals did not do it for me, so I have been trying to use RegEx, and came up with (^\d+.\d{2}). This however only identifies what I want to keep.
Is there a way to do this using Regex_Replace?
Thank you all in advance for your help!
Use
^(\d+\.\d{2})\d+$
Replacement: $1. See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to $1:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d{2} digits (0-9) (2 times)
--------------------------------------------------------------------------------
) end of $1
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

Regex to reject if all numbers and reject colon

I am trying for a regex to
reject if input is all numbers
accept alpha-neumeric
reject colon ':'
I tried ,
ng-pattern="/[^0-9]/" and
ng-pattern="/[^0-9] [^:]*$/"
for example ,
"Block1 Grand-street USA" must be accepted
"111132322" must be rejected
"Block 1 grand : " must be rejected
You may use
ng-pattern="/^(?!\d+$)[^:]+$/"
See the regex demo.
To only forbid a : at the end of the string, use
ng-pattern="/^(?!\d+$)(?:.*[^:])?$/"
See another regex demo
The pattern matches
^ - start of string
(?!\d+$) - no 1+ digits to the end of the string
[^:]+ - one or more chars other than :
(?:.*[^:])? - an optional non-capturing group that matches 1 or 0 occurrences of
.* - any 0+ chars other than line break chars, as many as possible
[^:] - any char other than : (if you do not want to match an empty string, replace the (?: and )?)
$ - end of string.
According to comments, you want to match any character but colon.
This should do the job:
ng-pattern="/^(?!\d+$)[^:]+$/"

StringRegExp pattern not working properly

I have this string:
{"name": "Fancy HaXXor123Name","profession": 1,"race": 2,"map_id": 1052,"world_id": 268435461,"team_color_id": 0,"commander": false,"fov": 0.768}
I want to get an array back which includes the following information (from left to right from the string):
Fancy HaXXor123Name
1
2
1052
268435461
0
false
0.768
I tried to mess with RegExBuddy and got a promissing pattern which looks like this
(\d{1,}).(\d{1,})|(\d{1,})|(?i)"(.*?)"
This is what I got back
name
Fancy HaXXor123Name
profession
1
race
2
map_id
10
2
world_id
2684354
1
team_color_id
0
commander
fov
0
768
So there are large spaces between the informations, torn numbers and the false is missing. I can't fix this problem and I'm completely new to StringRegExp.
I'm using AutoIT which uses the PCRE RegExp-Engine (this is what think).
You may use a regex like the following:
"\s*:\s*(?:"\K[^"]*|\K[^][\s,{}]+)
See the regex demo
Details:
"\s*:\s* - a literal ", 0+ whitespaces, :, 0+ whitespaces
(?:"\K[^"]*|\K[^][\s,{}]+) - A non-capturing group matching 2 alternatives:
"\K[^"]* - a ", then \K zeros the text matched so far, and then matches 0+ chars other than " with [^"]*
\K[^][\s,{}]+ - \K drops the text matched so far, and [^][\s,{}]+ matches 1+ chars other than ], [, whitespace, ,, { and }.

Lex/Flex - Split the phone number Up?

I am making a program which got to split the phone-number apart, each part has been divided by a hyphen (or spaces, or '( )' or empty).
Exp: Input: 0xx-xxxx-xxxx or 0xxxxxxxxxx or (0xx)xxxx-xxxx
Output: code 1: 0xx
code 2: xxxx
code 3: xxxx
But my problem is: sometime "Code 1" is just 0x -> so "Code 2" must be xxxxx (1st part always have hyphen or a parenthesis when 2 digit long)
Anyone can give me a hand, It would be grateful.
According to your comments, the following regex will extract the information you need
^\(?(0\d{1,2})\)?[- ]?(\d{4,5})[- ]?(\d{4})$
Break down:
^\(?(0\d{1,2})\)? matches 0x, 0xx, (0xx) and (0x) at he beggining of the string
[- ]? as parenthesis can only be used for the first group, the only valid separators left are space and the hyphen. ? means 0 or 1 time.
(\d{4,5}) will match the second group. As the length of the 3rd group is fixed (4 digits), the regex will automatically calculate the length of the Group1 and 2.
(\d{4})$ matches the 4 digits at the end of the number.
See it in action
You can the extract data from capture group 1,2 and 3
Note: As mentionned in the comments of the OP, this only extracts data from correctly formed numbers. It will match some ill-formed numbers.

Regex Function in salesforce

Could any of you pls explain the following code. For eg., Why D,d is used for?
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))
The double backslashes are used because of Java's string escaping rules. The pure regex means:
\D*? # Match any number of non-digit characters (the "?" is useless here)
( # Match...
\d # a single digit
\D*? # optionally followed by any number of non-digits (again, useless "?")
){10} # Repeat the previous group 10 times.
So this regex matches any string that contains exactly ten digits (plus any number of other, non-digit characters).
If you're using the REGEX from the example in Salesforce, it's useless. It matches "this1234567890that" where "this" and "that" can be any value. I used: NOT( REGEX(Phone, "\([0-9]{3}\) [0-9]{3}-[0-9]{4}|\d{10}")) to accomplish the desired behavior.
My version translates to:
\\( # Match '('
[0-9]{3} # Match 3 digits
\\) # Match ')' followed by a space
[0-9]{3} # Match 3 digits
- # Match hyphen
[0-9]{4} # Match 4 more digits
|\\d{10} # or match 10 digits instead of all the previous

Resources