What is a regular expression that accepts only characters ranging from a to z?
The pattern itself would be [a-z] for single character and ^[a-z]+$ for entire line. If you want to allow uppercase as well, make it [a-zA-Z] or ^[a-zA-Z]+$
Try this to allow both lower and uppercase letters in A-Z:
/^[a-zA-Z]+$/
Remember that not all countries use only the letters A-Z in their alphabet. Whether that is an issue or not for you depends on your needs. You may also want to consider if you wish to allow whitespace (\s).
Allowing only character and space in between words :
^[a-zA-Z_ ]*$
Regular Expression Library
^[A-Za-z]+$
To understand how to use it in a function to validate text, see this example
Use
^[a-zA-Z]$
and browse for more at Expressions in category: Strings.
Try to use this plugin for masking input...you can also check out the demo and use this plugin if this is what you may want...
Masked Input Plugin
As you can see in the demonstration that you can use both alphatbets and numbers in a combination for complex textbox validations where an user might want to type not only alphatbets(azAZ) but also with numbers too(ie. alphanumberics)...specific validations like accepting only numbers in particular format(eg.phone numbers) can be done...that is the case when you can use this plugin for different circumstances..
hope this helps...
Just for people using bash shell, instead of "+" use "*"
"^[a-zA-Z]*$"
None of the answers exclude special characters... Here is regex to ONLY allow letters, lowercase and uppercase.
/^[_A-zA-Z]*((-|\s)*[_A-zA-Z])*$/g
And as for different languages, you can use this function to convert letters to english letters before the check, just replace returnString.replace() with letters you need.
export function convertString(phrase: string) {
var maxLength = 100;
var returnString = phrase.toLowerCase();
//Convert Characters
returnString = returnString.replace("ą", "a");
returnString = returnString.replace("č", "c");
returnString = returnString.replace("ę", "e");
returnString = returnString.replace("ė", "e");
returnString = returnString.replace("į", "i");
returnString = returnString.replace("š", "s");
returnString = returnString.replace("ų", "u");
returnString = returnString.replace("ū", "u");
returnString = returnString.replace("ž", "z");
// if there are other invalid chars, convert them into blank spaces
returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
// convert multiple spaces and hyphens into one space
returnString = returnString.replace(/[\s-]+/g, " ");
// trims current string
returnString = returnString.replace(/^\s+|\s+$/g, "");
// cuts string (if too long)
if (returnString.length > maxLength) returnString = returnString.substring(0, maxLength);
// add hyphens
returnString = returnString.replace(/\s/g, "-");
return returnString;
}
Usage:
const firstName = convertString(values.firstName);
if (!firstName.match(allowLettersOnly)) {
}
Match any word that contains any character in this group: [a-zA-Z0-9_]
/^[\w]+$/
Eg.
Match: abcz, AbcZ, abc_1, AbC_1
No match: abc z abc-z Abc-z AbC-9 aBc,12
Related
I want to check if a string only contains:
Letters
Numbers
Underscores
Periods
in Flutter, I tried the following to get only the letters but even if other characters are there it returns true if it contains a letter:
String mainString = "abc123";
print(mainString.contains(new RegExp(r'[a-z]')));
As I told it returns true since it contains letters, but I want to know if it only contains letters.
Is there a way to do that?
The problem with your RegExp is that you allow it to match substrings, and you match only a single character. You can force it to require that the entire string be matched with ^ and $, and you can match against one or more of the expression with +:
print(RegExp(r'^[a-z]+$').hasMatch(mainString));
To match all the characters you mentioned:
print(RegExp(r'^[A-Za-z0-9_.]+$').hasMatch(mainString));
the basic way of doing this is as follow:
define a list of acceptable characters:
// for example
List<String> validChar = ["1", "2", "3", "t"];
loop through all character of your string and check its validity:
// given text
String x = "t5";
bool valid = true;
for(int i=0; i<x.length; i++){
if(!validChar.contains(x[i])){
valid = false;
}
}
print(valid);
just change the x and validChar as your need.
I want to use Regex to acquire some ID's in a cellstring array, the array looks like this:
myString = '(['US04650Y1001', 'US90274P3029', 'HON WI', 'US41165F1012'])';
My pattern for regex is as follows:
pattern = '[A-Za-z0-9.^_]+';
newArr = regexp(myString, pattern,'match');
I'd like to get the ID called 'HON WI', but with my current pattern, its splitting it into two because my pattern can't deal with the whitespace properly. I would like to get the whole "HON WI", as well as my other strings, everything that's in '', these might have special characters like ^, . or _, but I don't know how to add the whitespace.
I already tried stuff like this, without success:
pattern = '[A-Za-z0-9.^_\s]+';
My new array should have, in each cell, the strings/ID's contained in myString (US04650Y1001, US90274P3029, HON WI and US41165F1012) with dimensions 1x4.
Another approach that seems to work but not entirely sure:
myString = strrep(myString,'([','');
myString = strrep(myString,'])','');
myString = regexp(myString,',','split');
myString = strrep(myString,'''','');
This seems to get me what I want, but I would like to know how can I alter the regex on my first approach.
Many thanks in advance.
You may use a mere '([^']+)' regex and use 'tokens' to get the captures:
myString = '([''US04650Y1001'', ''US90274P3029'', ''HON WI'', ''US41165F1012''])';
pattern = '''([^'']+)''';
newArr = regexp(myString, pattern,'match', 'tokens');
The newArr will look like
{
[1,1] = 'US04650Y1001'
[1,2] = 'US90274P3029'
[1,3] = 'HON WI'
[1,4] = 'US41165F1012'
}
You may option is to use lookaround assertions. The following will match any string made of alphanumeric character or underscore (\w), space (' ') or characters . or ^, that is located between quotes. This will specifically exclude the blank space next to the comma, in the separation between tokens, i.e. ', ' does not give a match.
Note that \s will match any blank space character (including tab, newline), this is why a space is preferred here:
pattern2='(?<='')[\w.^ ]+(?='')';
pattern2 =
(?<=')[\w.^ ]+(?=')
newArr = regexp(myString, pattern2,'match');
newArr'
ans =
'US04650Y1001'
'US90274P3029'
'HON WI'
'US41165F1012'
Below is a example string -
$string = "abcde वायरस abcde"
I need to check weather this string contains any Hindi (Devanagari) content and if so the count of characters and words. I guess regex with unicode character class can work http://www.regular-expressions.info/unicode.html. But I am not able to figure out the correct regex statement.
To find out, if a string contains a Hindi (Devanagari) character, you need to have a full list of all Hindi characters. According to this website, the Hindi characters are the hexadecimal characters between 0x0900 and 0x097F (decimal 2304 to 2431).
The regular expression pattern needs to match, if any of those characters are in the set. Therefore, you can use a pattern (actually a set of characters) to match the string, which looks like this:
[\u0900\u0901\u0902 ... \u097D\u097E\u097F]
Because it is rather cumbersome to manually write this list of characters down, you can generate this string by iterating over the decimal characters from 2304 to 2431 or over the hexadecimal characters.
To count all words containing at least one Hindi character, you can use the following pattern. It contains white-space (\s) around the word or the beginning (^) or the end ($) around the word, and a global flag, to match every occurence (/g):
/(?:^|\s)[\u0900\u0901\u0902 ... \u097D\u097E\u097F]+?(?:\s|$)/g
Here is a live implementation in JavaScript:
var numberOfHindiCharacters = 128;
var unicodeShift = 0x0900;
var hindiAlphabet = [];
for(var i = 0; i < numberOfHindiCharacters; i++) {
hindiAlphabet.push("\\u0" + (unicodeShift + i).toString(16));
}
var regex = new RegExp("(?:^|\\s)["+hindiAlphabet.join("")+"]+?(?:\\s|$)", "g");
var string1 = "abcde वायरस abcde";
var string2 = "abcde abcde";
[ string1.match(regex), string2.match(regex) ].forEach(function(match) {
if(match) {
console.log("String contains " + match.length + " words with Hindi characters only.");
} else {
console.log("String does NOT contain any words with Hindi characters only.");
}
});
It should be a range. The list of all characters is not required.
The following will detect a Devanagari word
[\u0900-\u097F]+
I want to place a dash after every letter but my regex place a dash at the end too. How can I improve my regex?
String outputS = dnaString.replaceAll("(.{1})", "$1-");
(.)(?!$)
You can use this.Replace by $1.See demo.
https://regex101.com/r/gT6vU5/11
(?!$) uses negative lookahead to state that do not capture a character which is at end of string.
Without regex (that is faster):
String[] nucleotides = dnaString.split("");
String outputS;
int seqLength = nucleotides.length;
if (seqLength > 1) {
StringBuilder sb = new StringBuilder();
sb.append(nucleotides[0]);
for (int i = 1; i < seqLength; i++) {
sb.append("-");
sb.append(nucleotides[i]);
}
outputS = sb.toString();
} else {
outputS = dnaString;
}
I know this is an old question, but for completeness and future reference I would like to add this answer.
In Java 8 you can also use:
String.join("-",dnaString.toCharArray());
Explanation:
String.join(delimiter,objects...);
String.join(delimiter,array);
String.join(delimiter,Iterable);
These are used to join all objects to a single string with the delimiter as separator.
dnaString.toCharArray();
This is a method to get a String as an char array.
This replaces all special characters with underscore '_' except the last occurence of a special character in the string.
String name = "one-of-the dummy$ string:i.txt"; // input
name = name.replaceAll("[^a-zA-Z0-9](?=.*[^a-zA-Z0-9])", "_");
System.out.println(name);
//input: one-of-the dummy$ string:i.txt
//output: one_of_the_dummy__string_i.txt
This
(.)\B
doesn't match the last char.
See https://regex101.com/r/p0Z0zA/1
So, in your case, should be:
String outputS = dnaString.replaceAll("(.{1})\\B", "$1-");
Credits to pigreco.
I wanted to match the words in string with reverse order.
We wanted to put validation to prompt user, if name exists in reverse order.
For example:
If name column has the value, 'Viral,Tennis'
Now if user enters a new name with the value, 'Tennis,Viral'
Then how can we match reverse order of word using regex or some other way?
I am using C#.net for development.
You could take a look at the Regex.Split(String input, String regex) and do something like so:
String[] userEntry = Regex.Split(userString, "\\s+");
StringBuilder sb = new StringBuilder()
for (int i = userEntry.Length -1; i >= 0; i--)
{
sb.append(userEntry[i]).append(" ");
}
String result = sb.ToString();
//Do Validation
That would do the trick, however, you need to keep in mind that things will get a little bit messy if you do not want to change the order of special symbols such as the comma. You could easily remove those and do any validation without special symbols.
EDIT: It depends on what you mean by special symbols. The regex [^a-zA-z0-9]+ will match any character which is not a letter (upper or lower case) and which is also not a number. So you could easily do something like so:
string input = ...
string pattern = "[^a-zA-z0-9]+";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
The above should yield a string which is only made from letters and digits. White spaces will also be removed.