Regex expression to check if contains more than 2 repeated characters - regex

I am checking in my validation form if there are repeated more than two times the same character.
I have tried this expression ([a-zA-Z0-9])\1{2,} but it doesn't work properly because if I add aaA it founds the string and it shouldn't because "aaA" is permitted. Also it doesn't check if it is repeated a special character.
Here is how I applied my code:
this.form = this.formBuilder.group(
{
newpassword: new FormControl(
'',
Validators.compose([
Validators.required,
CustomValidators.patternValidator(/[(\[a-zA-Z0-9\])\\1{2,}]/, {
hasRepeatedCharacters: true,
}),
])
),
},
{ validators: this.password }
);
Any idea?

If I understand correctly what you are considering to be invalid, you want this:
/(.)\1{2,}/

Use the following regex to detect any character repeated 2 or more times:
(.)\1{2,}
In order to capture aaA (repeated letters irrespective of their case) as well, you'll need to add the case-insensitive i flag.

You can use /(.)(?=\1.*\1)/, assuming you allow the repeated characters to be non-consecutive:
const pat = /(.)(?=.*\1.*\1)/;
[
"a",
"aa",
"aaa",
"zba1a1za",
"aaA",
"aaAA",
"aAaAa",
"aAbbAb",
].forEach(e => console.log(`'${e}' => ${pat.test(e)}`));

Related

error while applying regex to numeric values to replace values in nifi

hi I have a data as below
[{
s1 = 98493456645
s2 = 0000000000
102 = 93234,
12 =
15 = rahdeshfui
16 = 2343432,234234
},
{
s1 = 435234235
s2 = 01
102 = 45336
12 =
15 = vjsfrh#gmail.com
16 = 2415454
}
]
now using reg expression i need to change to json format and i have tried this
regexp:- ([^\s]+?.*)=((.*(?=,$))+|.*).*
replace value:- "$1":"$2",
for this values i am getting output as below
[{
"s1":"98493456645",
"s2":"0000000000",
"102":"93234,",
"12":"",
"15":"rahdeshfui",
"16":"2343432,234234",
},
{
"s1":"435234235",
"s2":"01",
"102":"45336",
"12":"",
"15":"vjsfrh#gmail.com",
"16":"2415454"
}
]
but I my expected output should be as below
[{
"s1":98493456645,
"s2":0,
"102":93234,
"12":"",
"15":"rahdeshfui",
"16":"2343432,234234",
},
{
"s1":435234235,
"s2":01,
"102":45336,
"12":"",
"15":"vjsfrh#gmail.com",
"16":"2415454"
}
]
for numneric numbers their should not be in "" and if i have a value more than one 0 i need to replace it with single 0 and for some values i have , at end i need to skip , in case if i have one
It might be a bit cumbersome, but you want to replace multiple things so one option might be to use multiple replacements.
Note that these patterns do not take the opening [{ and closing ]] into account or any nesting but only the key value part as your posting pattern is for the example data.
1.) Wrap the keys and values in double quotes while not capturing the
comma at the end and match the equals sign including the surrounding
spaces:
(\S+) = (\S*?),?(?=\n) and replace with "$1":"$2",
Demo
2.) Remove the double quotes around the digits except for those that start with 0:
("[^"]+":)"(?!0+[1-9])(\d+)"" and replace with $1$2
Demo
3.) Remove the comma after the last key value:
("[^"]+":)(\S+),(?!\n *"\w+") and replace with $1$2
Demo
4.) Replace 2 or more times a zero with a single zero:
("[^"]+":)0{2,} and replace with $10
Demo
That will result in:
[{
"s1":98493456645,
"s2":0,
"102":93234,
"12":"",
"15":"rahdeshfui",
"16":"2343432,234234"
},
{
"s1":435234235,
"s2":"01",
"102":45336,
"12":"",
"15":"vjsfrh#gmail.com",
"16":2415454
}
]
Is assume the last value "16":"2415454" is "16":2415454 as the value contains digits only.

How to make regexp for multiple condition?

I have regexp code like below (I'm using VerbalExpression dart plugin ), My purpose is to check that a string starts with "36", followed by "01", "02", or "03". After that can be anything as long as the whole string is 16 characters long.
var regex = VerbalExpression()
..startOfLine()
..then("36")
..then("01")
..or("02")
..anythingBut(" ")
..endOfLine();
String nik1 = "3601999999999999";
String nik2 = "3602999999999999";
String nik3 = "3603999999999999";
print('result : ${regex.hasMatch(nik1)}');
print('Hasil : ${regex.hasMatch(nik2)}');
print('Hasil : ${regex.hasMatch(nik3)}');
my code only true for nik1 and nik2, however i want true for nik3, I noticed that i can't put or() after or() for multiple check, it just give me all false result, how do i achieve that?
I'm not familiar with VerbalExpression, but a RegExp that does this is straightforward enough.
const pattern = r'^36(01|02|03)\S{12}$';
void main() {
final regex = RegExp(pattern);
print(regex.hasMatch('3601999999999999')); // true
print(regex.hasMatch('3602999999999999')); // true
print(regex.hasMatch('3603999999999999')); // true
print(regex.hasMatch('360199999999999')); // false
print(regex.hasMatch('3600999999999999')); // false
print(regex.hasMatch('36019999999999999')); // false
}
Pattern explanation:
The r prefix means dart will interpret it as a raw string ("$" and "\" are not treated as special).
The ^ and $ represent the beginning and end of the string, so it will only match the whole string and cannot find matches from just part of the string.
(01|02|03) "01" or "02" or "03". | means OR. Wrapping it in parentheses lets it know where to stop the OR.
\S matches any non-whitespace character.
{12} means the previous thing must be repeated 12 times, so \S{12} means any 12 non-whitespace characters.

Why condition returns True using regular expressions for finding special characters in the string?

I need to validate the variable names:
name = ["2w2", " variable", "variable0", "va[riable0", "var_1__Int", "a", "qq-q"]
And just names "variable0", "var_1__Int" and "a" are correct.
I could Identify most of "wrong" name of variables using regex:
import re
if re.match("^\d|\W|.*-|[()[]{}]", name):
print(False)
else:
print(True)
However, I still become True result for va[riable0. Why is it the case?
I control for all type of parentheses.
.match() checks for a match only at the beginning of the string, while .search() checks for a match anywhere in the string.
You can also simplify your regex to this and call search() method:
^\d|\W
That basically checks whether first character is digit or a non-word is anywhere in the input.
RegEx Demo
Code Demo
Code:
>>> name = ["2w2", " variable", "variable0", "va[riable0", "var_1__Int", "a", "qq-q"]
>>> pattern = re.compile(r'^\d|\W')
>>> for str in name:
... if pattern.search(str):
... print(str + ' => False')
... else:
... print(str + ' => True')
...
2w2 => False
variable => False
variable0 => True
va[riable0 => False
var_1__Int => True
a => True
qq-q => False
Your expression is:
"^\d|\W|.*-|[()[]{}]"
But re.match() matches from the beginning of the string always, so your ^ is unnecessary, but you need a $ at the end, to make sure the entire input string matches, and not just a prefix.

Mongodb word count using map reduce

I have a problem with counting words
I want to count word in projects.log.subject.
ex) count [A],[B],[C]..
I searched how to use map reduce.. but I don't understand how to use it for result i want.
{
"_id": ObjectID("569f3a3e9d2540764d8bde59"),
"A": "book",
"server": "us",
"projects": [
{
"domainArray": [
{
~~~~
}
],
"log": [
{
~~~~~,
"subject": "[A][B]I WANT THIS"
}
],
"before": "234234234"
},
{
"domainArray": [
{
~~~~
}
],
"log": [
{
~~~~~,
"subject": "[B][C]I WANT THIS"
}
],
"before": "234234234"
},....
] //end of projects
}//end of document
This is a basic principle of using regular expressions and testing each string against the source string and emitting the found count for the result. In mapReduce terms, you want your "mapper" function to possibly emit multiple values for each "term" as a key, and for every array element present in each document.
So you basically want a source array of regular expressions to process ( likely just a word list ) to iterate and test and also iterate each array member.
Basically something like this:
db.collection.mapReduce(
function() {
var list = ["the", "quick", "brown" ]; // words you want to count
this.projects.forEach(function(project) {
project.log.forEach(function(log) {
list.forEach(function(word) {
var res = log.subject.match(new RegExp("\\b" + word + "\\b","ig"));
if ( res != null )
emit(word,res.length); // returns number of matches for word
});
});
});
},
function(key,values) {
return Array.sum(values);
},
{ "out": { "inline": 1 } }
)
So the loop processes the array elements in the document and then applies each word to look for with a regular expression to test. The .match() method will return an array of matches in the string or null if done was found. Note the i and g options for the regex in order to search case insensitive and beyond just the first match. You might need m for multi-line if your text includes line break characters as well.
If null is not returned, then we emit the current word as the "key" and the count as the length of the matched array.
The reducer then takes all output values from those emit calls in the mapper and simply adds up the emitted counts.
The result will be one document keyed by each "word/term" provided and the count of total occurances in the inspected field within the collection. For more fields, just add more logic to sum up the results, or similarly just keep "emitting" in the mapper and let the reducer do the work.
Note the "\\b" represents a word boundary expression to wrap each term escaped by` in order to construct the expression from strings. You need these to discriminate "the" from "then" for example, by specifying where the word/term ends.
Also that as regular expressions, characters like [] are reserved, so if you actually were looking for strings like that the you similarly escape, i.e:
"\[A\]"
But if you were actually doing that, then remove the word boundary characters:
new RegExp( "\[A\]", "ig" )
As that is enough of a complete match in itself.

Challenging regular expression

There is a string in the following format:
It can start with any number of strings enclosed by double braces, possibly with white space between them (whitespace may or may not occur).
It may also contain strings enclosed by double-braces in the middle.
I am looking for a regular expression that can separate the start from the rest.
For example, given the following string:
{{a}}{{b}} {{c}} def{{g}}hij
The two parts are:
{{a}}{{b}} {{c}}
def{{g}}hij
I tried this:
/^({{.*}})(.*)$/
But, it captured also the g in the middle:
{{a}}{{b}} {{c}} def{{g}}
hij
I tried this:
/^({{.*?}})(.*)$/
But, it captured only the first a:
{{a}}
{{b}} {{c}} def{{g}}hij
This keeps matching {{, any non { or } character 1 or more times, }}, possible whitespace zero or more times and stores it in the first group. Rest of the string will be in the 2nd group. If there are no parts surrounded by {{ and }} the first group will be empty. This was in JavaScript.
var str = "{{a}}{{b}} {{c}} def{{g}}hij";
str.match(/^\s*((?:\{\{[^{}]+\}\}\s*)*)(.*)/)
// [whole match, group 1, group 2]
// ["{{a}}{{b}} {{c}} def{{g}}hij", "{{a}}{{b}} {{c}} ", "def{{g}}hij"]
How about using preg_split:
$str = '{{a}}{{b}} {{c}} def{{g}}hij';
$list = preg_split('/(\s[^{].+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($list);
output:
Array
(
[0] => {{a}}{{b}} {{c}}
[1] => def{{g}}hij
)
I think I got it:
var string = "{{a}}{{b}} {{c}} def{{g}}hij";
console.log(string.match(/((\{\{\w+\}\})\s*)+/g));
// Output: [ '{{a}}{{b}} {{c}} ', '{{g}}' ]
Explanation:
( starts a group.
( another;
\{\{\w+\}\} looks for {{A-Za-z_0-9}}
) closes second group.
\s* Counts whitespace if it's there.
)+ closes the first group and looks for oits one or more occurrences.
When it gets any not-{{something}} type data, it stops.
P.S. -> Complex RegEx takes CPU speed.
You can use this:
(java)
string[] result = yourstr.split("\\s+(?!{)");
(php)
$result = preg_split('/\s+(?!{)/', '{{a}}{{b}} {{c}} def{{g}}hij');
print_r($result);
I donĀ“t know exactly why are you want to split, but in case that the string contains always a def inside, and you want to separate the string from there in two halves, then, you can try something like:
string text = "{{a}}{{b}} {{c}} def{{g}}hij";
Regex r = new Regex("def");
string[] split = new string[2];
int index = r.Match(text).Index;
split[0] = string.Join("", text.Take(index).Select(x => x.ToString()).ToArray<string>());
split[1] = string.Join("", text.Skip(index).Take(text.Length - index).Select(x => x.ToString()).ToArray<string>());
// Output: [ '{{a}}{{b}} {{c}} ', 'def{{g}}hij' ]