I am using this regex for handling all sort of names:
String Regex_Name="^([A-Za-z]*|\\p{L})+([ ]*|[A-Za-z]*|[']*|\\p{L}*)+([\\s]?[A-Za-z]*)+[A-Za-z]$";
While running the code I am getting this error:
Unknown character property name {​​​​​​​L} near index 44
^[A-Za-z][[A-Za-z]*\p{​​​​​​​L}​​​​​​​*[,]?[ ]?[-]?[A-Za-z]+]+([ ]?[.]?[,]?[(]?[A-Za-z]+[)]?[-]?\p{​​​​​​​L}​​​​​​​*)+([,]?|[.]?)$
How can I solve the issue?
Use
String Regex_Name="^\\p{L}+(?:[’'-]\\p{L}+)*(?:\\s+\\p{L}+(?:[’'-]\\p{L}+)*)*$";
See proof.
The expression does not support shortened, abbreviated names, like John G. Smith.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\p{L}+ any character of: letters (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[’'-] any character of: '’', ''', '-'
--------------------------------------------------------------------------------
\p{L}+ any character of: letters (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\p{L}+ any character of: letters (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
[’'-] any character of: '’', ''', '-'
--------------------------------------------------------------------------------
\p{L}+ any character of: letters (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Related
I'm starving to understand the logic of regex let's say I got this string
1 SM-TEST S/M-BLEU, 25.00 EA 96.00
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var test = ReadPdfFile("C:\\Users\\mducharme\\Desktop\\PO # 70882.pdf");
var result = Regex.Split(test, "\r\n|\r|\n");
foreach (var lines in result)
{
if (Regex.IsMatch(lines, #"^\d\s"))
{
string line = lines.ToString();
string pattern = #"^(\S+\s+\S+).*?,(?=\s*\d+\.\d+\b)";
string replacement = "$1";
string result2 = Regex.Replace(line, pattern, replacement);
System.Diagnostics.Debug.WriteLine(result2);
}
}
}
Each lines show a different value like the first one and so on
2 SM-BLABLA S-M-YELLOW, 50.00 EA 96.00...
In the end I want to show up in my MessageBox for the first value only
1 SM-TEST 25.00 EA 96.00
but the regex doesn't seems to do it's job compared to regex101 website code.
Thank you,
Use
^(\d+\s+(?:(?!\d+\.\d+\s+[xX]\s+\d+\.\d+)[A-Z0-9-])+).*?(?=\s\d+\.\d+\s)
See regex proof. Replace with $1.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ")
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
[xX] any character of: 'x', 'X'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ")
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[A-Z0-9-] any character of: 'A' to 'Z', '0' to
'9', '-'
--------------------------------------------------------------------------------
)+ end of grouping
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
I am not so into REGEX and I am finsing some problem to adapt this regex that verify a phone number to my use case.
I have this REGEX validating phone number with international prefix: https://www.regextester.com/97440
(([+][(]?[0-9]{1,3}[)]?)|([(]?[0-9]{4}[)]?))\s*[)]?[-\s\.]?[(]?[0-9]{1,3}[)]?([-\s\.]?[0-9]{3})([-\s\.]?[0-9]{3,4})
It correctly validates string as: +39 3298494333 but it doesn't validate string representing a number without the international prefix, for example this string doesn't match my regex 3298494333
How can be changed in order to accept also phone number that doesn't have the prefix?
Use this fix:
(?:(?:\+\(?[0-9]{1,3}|\(?\b[0-9]{4})\)?)?\s*\)?[-\s.]?\(?[0-9]{1,3}\)?[-\s.]?[0-9]{3}[-\s.]?[0-9]{3,4}
See proof.
Explanation
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
\+ '+'
--------------------------------------------------------------------------------
\(? '(' (optional (matching the most
amount possible))
--------------------------------------------------------------------------------
[0-9]{1,3} any character of: '0' to '9' (between
1 and 3 times (matching the most
amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\(? '(' (optional (matching the most
amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
[0-9]{4} any character of: '0' to '9' (4 times)
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\)? ')' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\)? ')' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[-\s.]? any character of: '-', whitespace (\n, \r,
\t, \f, and " "), '.' (optional (matching
the most amount possible))
--------------------------------------------------------------------------------
\(? '(' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[0-9]{1,3} any character of: '0' to '9' (between 1
and 3 times (matching the most amount
possible))
--------------------------------------------------------------------------------
\)? ')' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[-\s.]? any character of: '-', whitespace (\n, \r,
\t, \f, and " "), '.' (optional (matching
the most amount possible))
--------------------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
--------------------------------------------------------------------------------
[-\s.]? any character of: '-', whitespace (\n, \r,
\t, \f, and " "), '.' (optional (matching
the most amount possible))
--------------------------------------------------------------------------------
[0-9]{3,4} any character of: '0' to '9' (between 3
and 4 times (matching the most amount
possible))
I want to sort a email:password by first occurrence of each email.
Example list:
email#example.com:passsword1
email#example.com:passsword2
email#example.com:passsword3
email1#example.com:passsword1
email1#example.com:passsword2
email1#example.com:passsword2
So only
email#example.com:passsword1
email1#example.com:passsword1
should be kept as result.
With my limited Regex skills I worked out this one but I guess I misunderstand something:
^(.*)(\r?\n\1)+(?=:)
Use
^((.*:).*)(?:\r?\n\2.*)+
See proof, use g and m flags.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
.* any character except \n (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\r? '\r' (carriage return) (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
\2 what was matched by capture \2
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)+ end of grouping
I got this regex for my laravel URL validator and if i put a ".finance" domain it shows it's against the regex. What is wrong? All other tested domain endings work so far.
$regex = '/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/';
Replace [a-z]{2,5} with [a-z]{2,} to allow any two or more letters in the TLD.
Remove {1}, these are always unnecessary.
(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)? is way to repeptitive, shorten it with optional groups / chars to (?:https?:\/\/(?:www\.)?)?.
Use
/^(?:https?:\/\/(?:www\.)?)?[a-z0-9]+(?:[-.][a-z0-9]+)*\.[a-z]{2,}(?::[0-9]{1,5})?(\/.*)?$/
See proof.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
http 'http'
--------------------------------------------------------------------------------
s? 's' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
www 'www'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
[a-z0-9]+ any character of: 'a' to 'z', '0' to '9'
(1 or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[-.] any character of: '-', '.'
--------------------------------------------------------------------------------
[a-z0-9]+ any character of: 'a' to 'z', '0' to '9'
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
[a-z]{2,} any character of: 'a' to 'z' (at least 2
times (matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
[0-9]{1,5} any character of: '0' to '9' (between 1
and 5 times (matching the most amount
possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
( group and capture to \1 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)? end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
What is the best regex to get groups of keys, operator and values from a clause like the image below?
What I have done so far is not accurate and is only able to get the first group: (^.*?(=|!=)+([^.]*))
Use
(\w+(?:\.\w+)*)\s*(!=|=)\s*(\w+)
See proof
Explanation
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1
or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
!= '!='
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
= '='
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \3