TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Email Encoding
Bipin Patil
Sep 25
2015
Code
1.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
SampleConsoleApplica
using
System;
using
System.Collections.Generic;
using
System.IO;
using
System.Linq;
using
System.Text.RegularExpressions;
namespace
Program
{
public
class
EmailEncoding
{
private
static
Dictionary<
string
,
string
> m_keyValue =
new
Dictionary<
string
,
string
>();
static
void
Main(
string
[] args)
{
try
{
string
INPUT_FILE = @
"input.txt"
;
string
OUTPUT_FILE = @
"output.txt"
;
if
(!File.Exists(INPUT_FILE))
{
File.Create(INPUT_FILE).Close();
return
;
}
if
(!File.Exists(OUTPUT_FILE))
{
File.Create(OUTPUT_FILE).Close();
}
StreamReader streamReader =
new
StreamReader(INPUT_FILE);
using
(streamReader)
{
using
(StreamWriter streamWriter =
new
StreamWriter(OUTPUT_FILE,
false
))
{
string
line;
while
((line = streamReader.ReadLine()) !=
null
)
{
string
output =
string
.Empty;
if
(ValidateInput(line,
out
output))
{
streamWriter.WriteLine(output);
}
else
{
streamWriter.WriteLine(
"ERROR"
);
}
}
}
}
streamReader.Close();
streamReader.Dispose();
}
catch
(Exception)
{
}
}
public
static
bool
ValidateInput(
string
input,
out
string
modifiedValue)
{
modifiedValue = input;
bool
result = EmailValidate(input);
if
(!result)
{
var match = ValidateKey(input);
if
(match !=
null
&& match.Success)
{
modifiedValue = UpdateKeyValue(match.Value.ToString(), modifiedValue);
while
(
true
)
{
Match nextMatch = match.NextMatch();
if
(nextMatch ==
null
|| !nextMatch.Success)
break
;
modifiedValue = UpdateKeyValue(nextMatch.Value.ToString(), modifiedValue);
match = nextMatch;
}
result =
true
;
}
var keyMatch = ValidateReplaceKey(input);
//if (keyMatch.Success && match.Success)
// return false;
if
(keyMatch !=
null
&& keyMatch.Success)
{
modifiedValue = ReplaceKeyToValue(keyMatch.Value.ToString(), modifiedValue);
while
(
true
)
{
Match nextMatch = keyMatch.NextMatch();
if
(nextMatch ==
null
|| !nextMatch.Success)
break
;
modifiedValue = ReplaceKeyToValue(nextMatch.Value.ToString(), modifiedValue);
keyMatch = nextMatch;
}
result =
true
;
}
// After success, check whether modfied string is valid email id.
if
(result && EmailValidate(modifiedValue))
return
result;
else
result =
false
;
}
else
return
true
;
return
result;
}
private
static
string
UpdateKeyValue(
string
matchValue,
string
originalItem)
{
string
modifiedValue = originalItem;
string
captureValue = matchValue.Replace(
"{"
,
""
).Replace(
"}"
,
""
);
string
[] values = captureValue.Split(
' '
);
if
(values.Length > 1)
{
if
(m_keyValue.ContainsKey(values[0]))
m_keyValue[values[0]] = values[1];
else
m_keyValue.Add(values[0], values[1]);
}
modifiedValue = modifiedValue.Replace(matchValue, values[1]);
return
modifiedValue;
}
private
static
string
ReplaceKeyToValue(
string
matchValue,
string
originalItem)
{
string
modifiedValue = originalItem;
string
captureKey = matchValue.Replace(
"{"
,
""
).Replace(
"}"
,
""
);
if
(!m_keyValue.ContainsKey(captureKey))
m_keyValue.Add(captureKey,
string
.Empty);
modifiedValue = modifiedValue.Replace(matchValue, m_keyValue[captureKey]);
return
modifiedValue;
}
private
static
bool
EmailValidate(
string
line)
{
string
pattern = @
"^[a-z][a-z|0-9|]*?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"
;
Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase);
if
(match.Success)
return
true
;
return
false
;
}
private
static
Match ValidateKey(
string
line)
{
string
pattern = @
"{([a-z|0-9|]+ [a-z|0-9|.|]+)}"
;
Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase);
if
(match.Success)
return
match;
return
null
;
}
public
static
Match ValidateReplaceKey(
string
line)
{
string
pattern = @
"{([a-z|0-9|]+)}"
;
Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase);
if
(match.Success)
return
match;
return
null
;
}
}
}
C#