Last active
August 29, 2015 14:21
-
-
Save nstanevski/6a141f4ea388e83452a3 to your computer and use it in GitHub Desktop.
Homework#5 - Regular Expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
/* | |
* Write a program that reads a string from the console and replaces all series | |
* of consecutive identical letters with a single one. | |
*/ | |
class SeriesOfLetters | |
{ | |
static void Main() | |
{ | |
string input = Console.ReadLine(); | |
string pattern = @"(\w)\1+"; | |
string replacement = "$1"; | |
string result = Regex.Replace(input, pattern, replacement); | |
Console.WriteLine(result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
/* | |
* Write a program that replaces in a HTML document given as string all the tags | |
* <a href="…">…</a> with corresponding tags [URL href=…]…[/URL]. | |
* Print the result on the console. | |
*/ | |
class ReplaceATag | |
{ | |
static void Main() | |
{ | |
string input = Console.ReadLine(); | |
string pattern = @"<a\s+href=([^>]+)>([^<]+)</a>"; | |
Regex regex = new Regex(pattern); | |
string replacement = "[URL href=$1]$2[/URL]"; | |
string result = Regex.Replace(input, pattern, replacement); | |
Console.WriteLine(result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
/* | |
* Write a program to extract all email addresses from a given text. The text comes | |
* at the only input line. Print the emails on the console, each at a separate line. | |
* Emails are considered to be in format <user>@<host>, where: | |
* <user> is a sequence of letters and digits, where '.', '-' and '_' can appear | |
* between them. Examples of valid users: "stephan", "mike03", "s.johnson", "st_steward", | |
* "softuni-bulgaria", "12345". Examples of invalid users: ''--123", ".....", "nakov_-", | |
* "_steve", ".info". | |
* <host> is a sequence of at least two words, separated by dots '.'. Each word is sequence | |
* of letters and can have hyphens '-' between the letters. Examples of hosts: "softuni.bg", | |
* "software-university.com", "intoprogramming.info", "mail.softuni.org". | |
* Examples of invalid hosts: "helloworld", ".unknown.soft.", "invalid-host-", "invalid-". | |
*/ | |
class ExtractEmails | |
{ | |
static void Main() | |
{ | |
string input = Console.ReadLine(); | |
string emailPattern = @"\b([A-Za-z0-9]+?)[\w\-\.]*?[A-Za-z0-9]+?@[A-Za-z0-9]+?([\w\-\.]+)\2*?\.[\w]{2,}\b"; | |
Regex regex = new Regex(emailPattern); | |
MatchCollection matches = regex.Matches(input); | |
foreach (Match match in matches) | |
{ | |
Console.WriteLine(match.Value); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
/* | |
* Write a program that reads a keyword and some text from the console and prints | |
* all sentences from the text, containing that word. A sentence is any sequence | |
* of words ending with '.', '!' or '?'. | |
*/ | |
class SentenceExtractor | |
{ | |
static void Main() | |
{ | |
string keyword = Console.ReadLine().Trim(); | |
keyword = @"\b"+keyword+@"\b"; | |
string text = Console.ReadLine(); | |
// \u2019 and \u2013 are en-dash and apostrophy | |
string sentencePattern = @"[A-Z][A-Za-z\s\-\,\:\'\u2019\u2013]*[\.\?\!]"; | |
Regex sentenceRegex = new Regex(sentencePattern); | |
MatchCollection matches = sentenceRegex.Matches(text); | |
foreach (Match match in matches) | |
{ | |
string sentence = match.Value; | |
if (Regex.IsMatch(sentence, keyword)) | |
Console.WriteLine(sentence); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
//https://judge.softuni.bg/Contests/Practice/Index/34#2 | |
class ValidUsernames | |
{ | |
static void Main() | |
{ | |
string line = Console.ReadLine().Trim(); | |
string[] usernames = Regex.Split(line, @"[\s/\\\(\)]+"); | |
List<string> validUsernames = new List<string>(); | |
//store only valid usernames in the list | |
string validUsernamePat = @"^[A-Za-z]\w+$"; | |
foreach (string username in usernames) | |
{ | |
if (username.Length >= 3 && username.Length <= 25 | |
&& Regex.IsMatch(username, validUsernamePat)) | |
validUsernames.Add(username); | |
} | |
//find 2 consecutive ones with the max sum of their lengths: | |
int maxSumLen = -1, currentSumLen = -1, maxSumLenIndex = -1; | |
for (int i = 0; i < validUsernames.Count - 1; i++) | |
{ | |
currentSumLen = validUsernames[i].Length + validUsernames[i + 1].Length; | |
if (currentSumLen > maxSumLen) | |
{ | |
maxSumLen = currentSumLen; | |
maxSumLenIndex = i; | |
} | |
} | |
Console.WriteLine("{0}\n{1}", validUsernames[maxSumLenIndex], | |
validUsernames[maxSumLenIndex + 1]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
/* | |
* https://judge.softuni.bg/Contests/Practice/Index/84#2 judge score 100/100 | |
* Note: works for valid href attributes, i.e. attibute values enclosed in | |
* either single or double quotes | |
*/ | |
class ExtractHyperlinks | |
{ | |
static void Main() | |
{ | |
string line; | |
//put all the input into a single string: | |
StringBuilder html = new StringBuilder(); | |
while ((line = Console.ReadLine()) != "END") | |
html.Append(line); | |
string htmlStr = html.ToString(); | |
//firt match <a> tag: | |
Regex aPattern = new Regex(@"<\s*a\s+[^>]+?>", RegexOptions.IgnoreCase); | |
/*then extract href attribute value (works only for attributes enclosed | |
/* in single or double quotes) | |
*/ | |
Regex hrefVal = new Regex(@"href\s*=\s*([\u0022'])(.+?)\1"); | |
foreach (Match anchor in aPattern.Matches(htmlStr)) | |
{ | |
string anchorStr = anchor.Value; | |
Match m = hrefVal.Match(anchor.Value); | |
if (m.Success) | |
Console.WriteLine(m.Groups[2].Value); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
//https://judge.softuni.bg/Contests/Practice/Index/84#3 | |
class QuerryMess | |
{ | |
private static Dictionary<string, List<string>> dict = | |
new Dictionary<string, List<string>>(); | |
private static void ProcessQuery(string line) | |
{ | |
string url = @"^[^>?]+\?"; //catch and remove leading part, up to '?' | |
Regex urlRegex = new Regex(url); | |
string res = urlRegex.Replace(line, ""); | |
string[] pairsStr = res.Split('&'); | |
foreach (string pairStr in pairsStr) | |
{ | |
//replace encoded spaces with literal ones: | |
string spaceEnc = @"(\+|%20)"; | |
res = Regex.Replace(pairStr, spaceEnc, " "); | |
res = Regex.Replace(res, @"\s+", " "); | |
string[] keyValueAttr = res.Split('='); | |
string key = keyValueAttr[0].Trim(); | |
string value = keyValueAttr[1].Trim(); | |
if (dict.ContainsKey(key)) | |
dict[key].Add(value); | |
else | |
{ | |
List<string> values = new List<string>(); | |
values.Add(value); | |
dict[key] = values; | |
} | |
} | |
} | |
static void Main() | |
{ | |
string line; | |
do{ | |
line = Console.ReadLine(); | |
if (line == "END") | |
break; | |
ProcessQuery(line); | |
foreach (KeyValuePair<string, List<string>> pair in dict) | |
{ | |
string valueStr = string.Join(", ", pair.Value.ToArray()); | |
Console.Write("{0}=[{1}]", pair.Key, valueStr); | |
} | |
dict.Clear(); | |
Console.WriteLine(); | |
}while(true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Linq; | |
using System.IO; | |
// https://judge.softuni.bg/Contests/Practice/Index/56#2 judge score 88/100 | |
class UseYourChainsBuddy | |
{ | |
static void Main() | |
{ | |
Console.SetIn(new StreamReader(Console.OpenStandardInput(8192))); | |
string html = Console.ReadLine(); | |
StringBuilder text = new StringBuilder(); | |
//extract p tag content: | |
string pTagContents = @"<\s*p\s*>([^<]+?)<\s*\/p\s*>"; | |
Regex regexPCont = new Regex(pTagContents, RegexOptions.IgnoreCase); | |
Regex regexNotSmallLetter = new Regex(@"[^a-z0-9]"); | |
foreach (Match match in regexPCont.Matches(html)) | |
{ | |
String pContent = match.Groups[1].Value; | |
pContent = regexNotSmallLetter.Replace(pContent, " "); | |
pContent = Regex.Replace(pContent, @"\s+", " "); | |
text.Append(pContent); | |
} | |
//decrypt text: | |
for (int i = 0; i < text.Length; i++) | |
{ | |
char ch = text[i]; | |
if (Char.IsLower(ch)) | |
{ | |
if (ch >= 'a' && ch < 'n') | |
ch = (char)(ch + 13); | |
else | |
ch = (char)(ch - 13); | |
text[i] = ch; | |
} | |
} | |
Console.WriteLine(text); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
//https://judge.softuni.bg/Contests/Practice/Index/84#4 | |
class SemanticHTML | |
{ | |
static void Main() | |
{ | |
string line = Console.ReadLine(); | |
Regex openDiv = new Regex(@"<div\s+(.*?)(id|class)\s*=\s*\u0022(header|main|nav|article|section|aside|footer)\u0022(.*?)>", RegexOptions.IgnoreCase); | |
Regex closeDiv = new Regex(@"<\/\s*div\s*>\s*<!--\s*(header|main|nav|article|section|aside|footer)\s*-->", RegexOptions.IgnoreCase); | |
StringBuilder outLine = new StringBuilder(); | |
while (line != "END") | |
{ | |
//process opening div | |
Match matchOpenDiv = openDiv.Match(line); | |
if (matchOpenDiv.Success) | |
{ | |
string preAttrib = matchOpenDiv.Groups[1].Value.Trim(); | |
string tagName = matchOpenDiv.Groups[3].Value.Trim(); | |
string postAttrib = matchOpenDiv.Groups[4].Value.Trim(); | |
preAttrib = Regex.Replace(preAttrib, @"\s+", " "); | |
preAttrib = Regex.Replace(preAttrib, @"\s+", " "); | |
outLine.Clear(); | |
outLine.Append("<"); outLine.Append(tagName); | |
if (preAttrib.Length > 0) | |
outLine.Append(" " + preAttrib); | |
if (postAttrib.Length > 0) | |
outLine.Append(" " + postAttrib); | |
outLine.Append(">"); | |
Console.WriteLine(outLine); | |
} | |
else | |
{ | |
//process closing div | |
Match matchCloseDiv = closeDiv.Match(line); | |
if (matchCloseDiv.Success) | |
{ | |
string closeTagName = matchCloseDiv.Groups[1].Value; | |
Console.WriteLine(@"</{0}>", closeTagName); | |
} | |
else | |
{ | |
//all other lines are untouched | |
Console.WriteLine(line); | |
} | |
} | |
line = Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment