Email address validation using regex [duplicate] - regex

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
javascript email validation check condition issue
(2 answers)
Closed 9 years ago.
I am using the email validation as mentioned below :
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#[A-Za- z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
in this pattern i need (hypen, apostrophe, underscore, period) to be included.
For example - pqr.m.o'abc#xyz.com
Please suggest

You can use something like this :
^[_A-Za-z'\\.\\!'0-9-\\+]+(\\.[_A-Za-z0-9\\!\\.'-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})
REGEX: ^([_A-Za-z'\.\!'0-9-\\+]+(\.[_A-Za-z0-9\\!\\.'-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*\.[A-Za-z]{2,})$
Demo
use \. for . where \ is the escape character .
UPDATE
To remove consecutive special characters , you can use :
String ar[] ={ "pqr.m.o''abc#xyz.com","pqr.m.o'abc#xyz.com"};
String REGEX = "[_A-Za-z'\\.\\!'0-9-\\+]+(\\.[_A-Za-z0-9\\!\\.'-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
Pattern p = Pattern.compile(REGEX);
for(String theString:ar){
Matcher m = p.matcher(theString);
while (m.find()) {
String matched = m.group();
String regex = "([._!'-])\\1";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(matched);
if (!matcher.find()) {
System.out.println(matched);
}
}
}

Related

How to return/print matches on a string in RegEx in Flutter/Dart? [duplicate]

This question already has an answer here:
How to put all regex matches into a string list
(1 answer)
Closed 1 year ago.
I want to return a pattern through regEx in flutter every time it' found, I tested using the Regex operation it worked on the same string, returning the match after that included match 'text:' to '}' letters, but it does not print the matches in the flutter application.
The code I am using:
String myString = '{boundingBox: 150,39,48,25, text: PM},';
RegExp exp = RegExp(r"text:(.+?(?=}))");
print("allMatches : "+exp.allMatches(myString).toString());
The output print statement is printing I/flutter ( 5287): allMatches : (Instance of '_RegExpMatch', Instance of '_RegExpMatch')
instead of text: PM
Following is the screenshot of how it is parsing on regexr.com
Instead of using a non greedy match with a lookahead, I would suggest using a negated character class matching any char except } in capture group 1, and match the } after the group to prevent some backtracking.
\b(text:[^}]+)}
You can loop the result from allMatches and print group 1:
String myString = '{boundingBox: 150,39,48,25, text: PM},';
RegExp exp = RegExp(r"\b(text:[^}]+)}");
for (var m in exp.allMatches(myString)) {
print(m[1]);
}
Output
text: PM
You need to use map method to retrieve the string from the matches:
String myString = '{boundingBox: 150,39,48,25, text: PM},';
RegExp exp = RegExp(r"text:(.+?(?=}))");
final matches = exp.allMatches(myString).map((m) => m.group(0)).toString();
print("allMatches : $matches");

.NET Core - regex matches whole string instead of group [duplicate]

This question already has answers here:
Returning only part of match from Regular Expression
(4 answers)
Closed 2 years ago.
I tested my regex on regex101.com, it returns 3 groups
text :
<CloseResponse>SESSION_ID</CloseResponse>
regex :
(<.*>)([\s\S]*?)(<\/.*>)
in C#, I get only one match and one group that contains the whole string instead of just the SESSION_ID
I expect the code to return only SESSION_ID
I tried finding a global option but there don't seem to be any
here is my code
Regex rg = new Regex(#"<.*>([\s\S]*?)<\/.*>");
MatchCollection matches = rg.Matches(tag);
if (matches.Count > 0) ////////////////////////////////// only one match
{
if (matches[0].Groups.Count > 0)
{
Group g = matches[0].Groups[0];
return g.Value; //////////////////// = <CloseResponse>SESSION_ID</CloseResponse>
}
}
return null;
thanks for helping me on this
I managed to make it work this way
string input = "<OpenResult>SESSION_ID</OpenResult>";
// ... Use named group in regular expression.
Regex expression = new Regex(#"(<.*>)(?<middle>[\s\S]*)(<\/.*>)");
// ... See if we matched.
Match match = expression.Match(input);
if (match.Success)
{
// ... Get group by name.
string result = match.Groups["middle"].Value;
Console.WriteLine("Middle: {0}", result);
}
// Done.
Console.ReadLine();
Use non-capturing group if you want whole string as result: (?:)
(?:<.*>)(?:[\s\S]*?)(?:<\/.*>)
Demo
If you just want to capture session id use this:
(?:<.*>)([\s\S]*?)(?:<\/.*>)
Demo

How to use \R in java 8 regex [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 5 years ago.
I am trying to use the new \R regex matcher from java 8.
However, for the following code :
public static void main(String[] args)
{
String s = "This is \r\n a String with \n Different Newlines \r and other things.";
System.out.println(s);
System.out.println(Pattern.matches("\\R", s));
if (Pattern.matches("\\R", s)) // <-- is always false
{
System.out.println("Matched");
}
System.out.println(s.replaceAll("\\R", "<br/>")); //This is a String with <br/> Different Newlines <br/> and other things.
}
The Pattern.matches always returns false, where as the replaceAll method does seem to find a match and does what I want it to. How do I make the Pattern.matches work ?
I have also tried the long about way and still can't get it to work :
Pattern p = Pattern.compile("\\R");
Matcher m = p.matcher(s);
boolean b = m.matches();
System.out.println(b);
Well matches (both in String and Matchers classes) attempts to match the complete input string.
You need to use matcher.find instead:
Pattern p = Pattern.compile("\\R");
Matcher m = p.matcher(s);
boolean b = m.find();
System.out.println(b);
From Java docs:
\R Matches any Unicode line-break sequence, that is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]
PS; If you want to know if input contains a line-break then this one liner will work for you:
boolean b = s.matches("(?s).*?\\R.*");
Note use of .* on either side of \R to make sure we are matching complete input. Also you need (?s) to enable DOTALL mode to be able to match multiline string with .*

Regex match in string [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I would like to extract two numbers for a strings by regex "[0-9]+"
var str = "ABcDEFG12345DiFKGLSG938SDsFSd"
What I want to extract is "12345" and "938".
But I am not sure how to do so in Kotlin.
This should work:
import java.util.regex.Matcher
import java.util.regex.Pattern
fun main(args:Array<String>) {
val p = Pattern.compile("\\d+")
val m = p.matcher("ABcDEFG12345DiFKGLSG938SDsFSd")
while (m.find())
{
println(m.group())
}
}
Pattern.compile("\\d+"), it will extract the digits from the expression.

Getting value using regex [duplicate]

This question already has answers here:
Get values between curly braces c#
(3 answers)
Closed 7 years ago.
how to get the value between first { and last } from a string which have multiple {}.
eg string: ".....[object:{ ..{...{..}...}..}]"
My approach using C#:
line="abcd..efg..[object:{ ab{..c{d.}.e.}f....g}]"
string p = ".*\\[Object:{([A-Za-z{}]*)}\\]";
Regex r = new Regex(p);
Match m=r.match(line);
string value=m.Groups[1].Value.ToString();
Result should be:
value= ab{..c{d.}.e.}f....g
{.*}
or
(?<={).*(?=})
This should do the trick for you.See demo
string strRegex = #"{.*}";
Regex myRegex = new Regex(strRegex, RegexOptions.Multiline);
string strTargetString = #".....[object:{ ..{...{..}...}..}]";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}