I need to create an regex that accepts only numbers, hypens (-), plus sign (+), spaces along with round brackets ( ).
I tried the regex (?\+?\d+)?[-.\s]?\d+[-.\s]?\d+.
What is need is like (123) 123-123-123, (+91) 123-123-1234 or 1234567890, or 123 123 1234 or +92 345 345 3456 or +92 123-123-1234.
Your regex starts with (?\+?\d+)? which is not correct. Maybe you were looking for (?:\+?\d+)?[-.\s]?\d+[-.\s]?\d+ which makes the first group non capturing.
If you want to match only numbers, hyphens (-), plus sign (+), spaces along with round brackets ( ) one or more times (so ++++ would also match) you could use:
^[-+\d() ]+$
If you want to narrow down your match for your provided strings maybe this could be an option.
There are 2 parts:
The optional part at the beginning (123) or (+91) or +92
A whitespace followed by either a format like 123-123-123 with hyphens or without hyphens 123 123 1234
^(?:\+\d+|\(\+?\d+\))?(?:\d{10}| ?\d{3}([ -])\d{3}\1\d{3,4})$
Explanation
From the beginning of the string ^
A non capturing group (?:
Which matches either a plus sign and one or more digits \+\d+
Or |
Match parenthesis with an optional plus sign, one or more digits and parenthesis \(\+?\d+\)
Close the non capturing group and make it optional )?
A non capturing group (?:
Which matches either 10 digits \d+{10}
Or |
Optional whitespace and digits pattern ?\d{3}([ -])\d{3}\1\d{3,4} which uses a capturing group \1 for the whitespace or the hyphen.
Close the non capturing group )
The end of the string $
Related
I am having a hard time finding a solution for negative accounting/money values that look like:
(1.00)
(100)
(100.00)
I've tried this, but for some reason, it's allowing values such as 'abcd'.
^0\.00||(\(\d*(?:\.\d{1,2})?\))?$
It does not match abcd, but if you test if the string has a match, then it will be true as there are positions that match.
As all the digits are optional at the end of the string, there is a position that can match.
Also the || matches a position after 0.00, before and after the digits part and on every position in abcd or an empty string.
You can change the || to |, match at least a single digit and use the alternation for both alternatives:
^(?:0\.00|\(\d+(?:\.\d{1,2})?\))$
^ Start of string
(?: Non capture group for the alternation
0\.00 Match 0.00
| Or
\(\d+(?:\.\d{1,2})?\) Match ( 1+ digits, optionally . and 1-2 digits and )
) Close non capture group
$ End of string
Regex demo
So a sensor I'm interfacing to either outputs 4 multi-digit integers (separated by spaces) or an error string.
Ideally my regex would return a match for either of the above scenarios and reject any
other outputs - e.g. if only 3 numbers are output. I can then check if there are 4 groups (number output) or 1 group (error string output) in the following c#.
The regex I have matches all the time and returns spaces when there are less than 4 numbers so I still need to check everything.
I've tried putting in ?: but the format breaks. Any regex whizzes up to the challenge? Thanks in advance.
([0-9]+\s)([0-9]+\s)([0-9]+\s)([0-9]+)|([a-zA-Z\s_!]+)
So a numeric example would be 11 222 33 4444 or Sensor is in an error state! An incorrect output would be 222 11 3333 as it only has 3 fields
Also - I need to capture the four numbers (but not the spaces) or the error string.
You can capture either the 4 groups with only digits and match the whitespace chars outside of the group.
Or else match 1+ times any of the listed characters in the character class. Note that \s can also match a newline, and as the \s is in the character class the match can also consist of only spaces for example.
To match the whole string, you can add anchors.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|[a-zA-Z\s_!]+)$
» Regex demo
Another option to match the error string, is to start matching word characters without digits, optionally repeated by a whitespace char and again word characters without digits.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|[^\W\d]+(?:\s+[^\W\d]+)*!?)$
» Regex demo
If there can be a digit in the error message, but you don't want to match only digits or whitespace chars, you can exclude that using a negative lookahead.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|(?![\d\s]*$).+)$
» Regex demo
I have following regular expression
(\w)+(,(\w)+)*
which is comma separated characters and numbers only
test123,test3,test9
I want to also add symbols like #, #, $ that can be used within \w
when i try [(\w)$#] not worked.
I need to use it in DevExpress TextEdit Mask. it says syntax error
http://prntscr.com/pbyq7p
There is a reply at the bottom if this page which mentions that special characters cannot be used within [].
The available character are listed on Mask Type: Extended Regular Expressions
The advice is to use grouping with an alternation to separate the character class and the special character.
You might try
(\w+|[##$]+)+(,(\w+|[##$]+))+
In parts
( Group 1
\w+ Match 1+ word chars
| Or
[##$]+ Match 1+ times any of the lister
)+ Close group and repeat 1+ times
( Group 2
, Match literally
(\w+|[##$]+) Same pattern as group 1
)+ Close group and repeat the whole group starting with , 1+ times
Regex demo
If your data only consists of characters a-z and numbers only, you could also try
([a-z0-9##$]+)+(,([a-z0-9##$]+))+
Regex demo
I need to match a string under the following conditions using Regex in C#:
Entire string can only be alphanumeric (including spaces).
Must be a maximum of 15 characters or less (including spaces).
First & last characters can only be a letter.
A single space can appear multiple times in anywhere but the first and last characters of the string. (Multiple spaces together should not be allowed).
Capitalization should be ignored.
Should match the WHOLE word(s).
If any one of these preconditions are broken, a match should not follow.
Here is what i currently have:
^\b([A-z]{1})(([A-z0-9 ])*([A-z]{1}))?\b$
And here are some test strings that should match:
Stack OverFlow
Iamthe greatest
A
superman23s
One Two Three
And some that shouldn't match (note the spaces):
Stack [double_space] Overflow Rocks
23Hello
ThisIsOver15CharactersLong
Hello23
[space_here]hey
etc.
Any suggestions would be much appreciated.
You should use lookaheads
|->matches if all the lookaheads are true
--
^(?=[a-zA-Z]([a-zA-Z\d\s]+[a-zA-Z])?$)(?=.{1,15}$)(?!.*\s{2,}).*$
-------------------------------------- ---------- ----------
| | |->checks if there are no two or more space occuring
| |->checks if the string is between 1 to 15 chars
|->checks if the string starts with alphabet followed by 1 to many requireds chars and that ends with a char that is not space
you can try it here
Try this regex: -
"^([a-zA-Z]([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])$"
Explanation : -
[a-zA-Z] // Match first character letter
( // Capture group
[ ](?=[a-zA-Z0-9]) // Match either a `space followed by non-whitespace` (Avoid double space, but accept single whitespace)
| // or
[a-zA-Z0-9] // just `non-whitespace` characters
){0,13} // from `0 to 13` character in length
[a-zA-Z] // Match last character letter
Update : -
To handle single characters, you can make the pattern after 1st character optional as nicely pointed by #Rawling in comments: -
"^([a-zA-Z](([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])?)$"
^^^ ^^^
use a capture group make it optional
And my version, again using look-aheads:
^(?=.{1,15}$)(?=^[A-Z].*)(?=.*[A-Z]$)(?![ ]{2})[A-Z0-9 ]+$
explained:
^ start of string
(?=.{1,15}$) positive look-ahead: must be between 1 and 15 chars
(?=^[A-Z].*) positive look-ahead: initial char must be alpha
(?=.*[A-Z]$) positive look-ahead: last char must be alpha
(?![ ]{2}) negative look-ahead: string mustn't contain 2 or more consecutive spaces
[A-Z0-9 ]+ if all the look-aheads agree, select only alpha-numeric chars + space
$ end of string
This will also need the IgnoreCase option setting
I am trying to create a RegEx expression that will successfully parse the following line:
"57" "testing123" 82 16 # 13 26 blah blah
What I want is to be able to do is identify the numbers in the line. Currently, what I'm using is this:
[0-9]+
which parses fine. However, where it gets tricky is if the number is in quotes, like "57" is or like "testing123" is, I do not want it to match.
In addition to that, anything after the hash sign (the '#"), I do not want to match anything at all after the hash sign.
So in this example, the matches I should be getting are "82" and "16". Nothing else should match.
Any help on this would be appreciated.
It should be easier for you to build 3 different regexes, and then create the logic that combines them:
Check, whether the string has #, and ignore everything after it.
Check, for all the matches of "\d+", and ignore all of them
Check everything that's left, whether it matches [0-9]+
.Net regular expression can rather easily parse this string. The following pattern should match everything until the comment:
\A # Start of the string
(?>
(?<Quoted> # A quoted string
"" # Open quotes
[^""\\]* # non quotes or backslashes
(?:\\.[^""\\]*)* # but allow escaped characters
"" # Close quotes
)
|
(?<Number> # A number
\d+ # some digits
)
|
\s+ # Whitespace separator
)*
If you also want to match the comment, add:
(?<Comment>
\# .*
)?
\z
You can get your numbers in a single Match, using all captures of the "Number" group:
Match parsed = Regex.Match(s, pattern, RegexOptions.IgnorePatternWhitespace);
CaptureCollection numbers = parsed.Groups["Number"].Captures;
Missing from this pattern is mainly unquoted string tokens, such as 4 8 this 15that, which can add some complexity, depending on how we'd want it to work.