How do I write a regular expression for checking if the entered value are all white space or empty or digits. In all other cases it will return false. I tried
\s*|\d+ which is allowing character values too.
Your regex \s*|\d+ just matches 0 or more whitespace or 1 or more digits anywhere inside an input string and it may contain whitespace mixed with digits and other characters.
You can use the following regex:
^(?:\s+|\d+)?$
See demo
The regex matches:
^ - start of string
(?:\s+|\d+)? - 1 or 0 occurrences (due to (?:...)?) of...
\s+ - 1 or more whitespace symbols or
\d+ - 1 or more digits
$ - end of string
In plain human words,
^....$ - make sure we require a full string match, any partial matches inside a string are not allowed
(?:...)? - all the texts matched with the subpatterns inside this group are optional, thus allowing an empty string match (i.e. return true if the string is empty).
Related
I am working on a text processing Api in java. I need to match the strings which are:
At least 8 characters in length.
Should only contain uppercase letters, lowercase letters or spaces.
Spaces should not be present in between the letters. They can however be leading or trailing. The String can also contain only spaces which are at least 8.
Regular expression which I tried but failed:
^\s*[a-zA-Z]{8,}\s*$
Demo of my tries in here.
Any help will be welcomed.
You can use the below regex to achieve your result:
^(?=.{8,}) *[a-zA-Z]* *$
Explanation of the above regex:
^ - denotes start of the test String.
(?=) - Positive lookahead.
.{8,} - any character other than newline with length at least 8.
* - 0 or more spaces in order to match the leading spaces.(\s is avoided)
[a-zA-Z]* - 0 or more letters (uppercase or lowercase). (You can use [a-z]* along with i(case insensitive) flag. Although, there will be no effect on performance.)
* - 0 or more spaces in order to match the trailing spaces.(\s is avoided)
$ - denotes end of the test String.
Above regex demo.
I am trying to check if string contains one extra character OR one digit. But for some reason it doesn't work in Swift but it works on online debuggers/editors.
This is my regex: ^(?=.*[a-z])(?=.*[$#$#!%*?&])|(?=.*[0-9]).{6,}
As I understand then:
^ - this is start anchor
(?=.*[a-z]) - string has one character
(?=.*[$#$#!%*?&]) - string has one special character
(?=.*[0-9]) - string has one digit
{6,} - string length is at least 6
and in regex validations the regular | symbol symbolises OR condition.
My question is that why the output is not what I want?
stackoverflow - false
stackoverflow1 - true
stackoverflow# - false // should be true
stackoverflow1# - false // should be true
This is what I do in swift:
func isPasswordValid(_ password : String) -> Bool{
let passwordTest = NSPredicate(format: "SELF MATCHES %#", "^(?=.*[a-z])(?=.*[$#$#!%*?&])|(?=.*[0-9]).{6,}$")
return passwordTest.evaluate(with: password)
}
To be clear, this is what I want: String must be at least 6 characters long and has at least one digit OR extra character. It would be good not to limit the characters to only (?=.*[$#$#!%*?&])
The pattern you have matches: 1) the start of a string that contains at least 1 lowercase ASCII letter and at least 1 of the special chars inside [...], 2) or matches a string of 6 or more chars that has at least 1 digit.
Also, please pay attention to the fact that MATCHES pattern requires the entire string to match, and that is the main reason for your pattern to fail in Swift.
It seems you want to match a string that has 6 or more chars, and should contain at least 1 digit or a special char (I see it can be either punctuation or symbol).
So, you need to use
"(?=.*[0-9\\p{S}\\p{P}]).{6,}"
See the regex demo. No need for ^ and $ as the MATCHES pattern requires a full string match.
Pattern details
(?=.*[0-9\\p{S}\\p{P}]) - a positive lookahead that requires that there should be an ASCII digit (0-9), punctuation (\p{P}) or a symbol (\p{S}) after any 0+ chars other than line break chars (prepend the pattern with (?s) to allow . match line break chars)
.{6,} - any six or more characters.
I've wrote a regex:
/^[a-zA-Z\-\_\. ]{2,60}$/
It does work fine-ish however it allows --- or ___ or ... or even -_. to be entered as an input (without 2 alpha at least) and I don't want that. For instance I can have -aa, a-a, aa--- (similarly for the other characters).
The requirement is that there should be at least 2 alpha in the string, and the hyphens and the other 2 non-alpha symbols mentioned can be anywhere inside the string.
Use
/^(?=(?:[^a-zA-Z]*[a-zA-Z]){2})[-_. a-zA-Z]{2,60}$/
See the regex demo
Details:
^ - start of string
(?=(?:[^a-zA-Z]*[a-zA-Z]){2}) - at least 2 alpha chars in the string (that is, there must be exactly 2 consecutive occurrences of:
[^a-zA-Z]* - zero or more chars other than ASCII letters
[a-zA-Z] - an ASCII letter)
[-_. a-zA-Z]{2,60} - 2 to 60 occurrences of allowed chars
$ - end of string
Note you do not need to escape - if it is at the start/end of the character class. _ is a word char, no need escaping it anywhere. The . does not need escaping inside a character class.
To tell the regex engine to limit ., _ and - chars to max 10 in the string, add (?!(?:[^._-]*[._-]){11}) negative lookahead after ^ anchor:
/^(?!(?:[^._-]*[._-]){11})(?=(?:[^a-zA-Z]*[a-zA-Z]){2})[-_. a-zA-Z]{2,60}$/
I the below items i want to only detect the valid items with regular expression.
Space in word means invalid, # sign means invalid, Starting word with number is invalid.
Invalid : M_123 ASD
Invalid : M_123#ASD
Invalid : 1_M# ADD
Valid : M_125ASD
Valid : M_125$ASD
I am trying as below :
[A-Za-z0-9_$]
Not working properly. I need to set both valid and invalid sets for a word.
Can i do a match with regular expression?
Your regex [A-Za-z0-9_$] presents a character class that matches a single character that is either an ASCII letter or digit, or _ or $ symbols. If you use it with std::regex_match, it would only match a whole string that consists of just one char like that since the pattern is anchored by default when used with that method. If you use it with an std::regex_search, a string like ([_]) would pass, since the regex is not anchored and can find partial matches.
To match 0 or more chars, you need to add * quantifier after your class. To match one or more chars, you need to add + quantifier after your character class. However, you have an additional restriction: a digit cannot appear at the start.
It seems you may use
^[A-Za-z][A-Za-z0-9_$]*$
See the regex demo at regex101.com.
Details:
^ - start of string
[A-Za-z] - an ASCII letter (exactly one occurrence)
[A-Za-z0-9_$]* - 0+ ASCII letters, digits, _ or $
$ - end of string anchor.
Note that with regex_match, you may omit ^ and $ anchors.
So the requirements are
cannot start with number( i am assuming it as start with alphabet)
cannot contain space or #
all other characters are valid
you can try this regex ^[a-zA-Z]((?![\# ]).)+?$
^[a-zA-Z] checks for alphabet at start of the line
((?![\# ]).)+?$ checks if there are no # or space in the remaining part of the line.
Online demo here
EDIT
As per Wiktor's comment the regex can be simplified to ^[a-zA-Z][^# ]+$.
I have a long input string that contains certain field names in-bedded in it. For instance:
SELECT some-name, some-name FROM [some-table] WHERE [some-column] = 'some-value'
The actual field name may change, but it is always in the form of word-word. I need to perform a regex replace on the string so that the output will look like this:
SELECT some - name, some - name FROM [some-table] WHERE [some-column] = 'some - value'
In other words, when the field name is enclosed in square-brackets, it should be left untouched, but when it is not, spaces should be inserted on either side of the dash. There are no nested square brackets and the reserved word could be one or more in the string.
You can do this:
Regex.Replace(input, "(?<!\[[^-\]]*)(\w+)-(\w+)(?![^-\]]*\])", "$1 - $2")
Here's an explanation of the pattern:
(?<!\[[^-\]]*) - This is a negative look-behind. It asserts that matches cannot be immediately preceded by text that matches the sub-pattern \[[^-\]]*. In other words, the matches we are looking for cannot be preceded by a [ character followed by any number of characters that are not a - or a ].
(\w+)-(\w+) - Matches one or more word-characters, then a dash, and then one or more word characters following the dash. By enclosing the sub-patterns on either side of the dash in capturing groups, we can then refer to their values as $1 and $2 in the replacement pattern.
(?![^-\]]*\]) - This is a negative look-ahead. Similar to the negative look-behind, it asserts that matches cannot be immediately followed by text which matches the sub pattern [^-\]]*\]. In other words, a match cannot be followed by any number of characters that are not a - or a ] and then a closing ].
See a demo.
At first glance, you might assume that you could simply assert that is must not be immediately preceded by a [ character and that it must not be immediately followed by a ] character. In other words, (?<!\[)(\w+)-(\w+)(?!\]). However, that pattern would still match the text ome-nam in the input [some-name] because the text ome-nam is not immediately preceded or followed by the brackets.
Dim regex As Regex = New Regex("\[[^-]*-[^-]*\]")
Dim match As Match = regex.Match("A long string containing square brackets [some-name]")
If match.Success Then
Console.WriteLine(match.Value)
End If
Or you could use Regex.IsMatch:
Return Regex.IsMatch("A long string containing square brackets [some-name]",
"\[[^-]*-[^-]*\]")
You may match and capture the [...] substrings and then only match hyphens that are not surrounded with hyphens to replace them:
Dim nStr As String = "SELECT 'some-name' FROM [some-name]"
Dim nResult = Regex.Replace(nStr, "(\[.+?])|\s*-\s*", New MatchEvaluator(Function(m As Match)
If m.Groups(1).Success Then
Return m.Groups(1).Value
Else
Return " - "
End If
End Function))
So, what is happening is:
(\[[^]]+]) - matches and stores the value of [...] substring inside the Group(1) buffer (or \[.+?] can be used here to match a [, then 1 or more any characters and then ] - with RegexOptions.Singleline flag so that . could match a newline, too)
(?<!\s)-(?!\s) - matches any hyphen not preceded ((?<!\s)) or followed ((?!\s)) with whitespace (\s). Actually, we may even use \s*-\s* (where \s* stands for zero or more whitespaces as many as possible since * is a greedy quantifier matching zero or more occurrences of the quantified subpattern) here to remove any whitespace there is to make sure we just insert 1 space before and after -.
If Group 1 matches, then we just re-insert it (Return m.Groups(1).Value), else we insert the space-enclosed hyphen Return " - ".
Just to check if it exists, you could try
\[[^\]]+-[^\]]+\]
It matches a literal [ and then any characters, except ], up to (including) a hyphen. Then again any characters, except ], up to a literal ].
See it here at regex101.
Actually I don't know the vb.net syntax but you can use regex as
/[\s\'](\w+)\-(\w+)/g
find the (\w+)-(\w+) which is followed by space or ' and replace your string with capture group 1st - 2nd
See the sample here