import Foundation
let pattern = #"back up code"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
//9-digits phone number
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
int n;
string s;
cin >> n;
getline(cin,s);
while(n--){
regex phone10("09[0-9]{2}(\\.[0-9]{3}){2}");
string st;
getline(cin, st);
if(regex_match(st, phone10))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
//11 digits phone number
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex phone10("01[0-9]{9}");
string st;
int n;
cin >> n;
getline(cin,st);
while(n--){
getline(cin, st);
if(regex_match(st, phone10))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
//alphabet string
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex pattern("[a-zA-Z\\s]+");
string st;
int n;
cin >> n;
getline(cin,st);
while(n--){
getline(cin, st);
if(regex_match(st, pattern))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
//Alpha-numberic Strings With Limited Length
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex pattern("[a-z0-9]{6,20}");
string st;
int n;
cin >> n;
getline(cin,st);
while(n--){
getline(cin, st);
if(regex_match(st, pattern))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
//bound checking for a Product
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
unsigned int a,b;
scanf("%u%u",&a,&b);
if(a!=0 && b!=0 && UINT_MAX/a<b)
printf("No");
else
printf("Yes");
}
//bound checking for a Signed Product
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
if(a>0&&b>0){
if(INT_MAX/a<b)
printf("No");
else
printf("Yes");
}
else if(a<0&&b<0){
if(INT_MAX/b>a)
printf("No");
else
printf("Yes");
}
else if(a>0&&b<0){
if(INT_MIN/a>b)
printf("No");
else
printf("Yes");
}
else if(a<0&&b>0){
if(INT_MIN/b>a)
printf("No");
else
printf("Yes");
}
else
printf("Yes");
}
//bound checking for a Signed sum
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
if(a>0 && b>0 && b>INT_MAX-a)
printf("No");
else if(a<0 && b<0 && b<INT_MIN-a)
printf("No");
else
printf("Yes");
}
//Bound checking for a Sum
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
unsigned int a,b;
scanf("%u%u",&a,&b);
if(UINT_MAX-a>=b)
printf("Yes");
else
printf("No");
}
//bound checking for an Integer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *s1 = (char*)malloc(12*sizeof(char));
char *s2 = (char*)malloc(12*sizeof(char));
gets(s1);
int a = atoi(s1);
sprintf(s2,"%d",a);
if(strcmp(s1,s2)==0)
printf("Yes");
else
printf("No");
}
//bound checking for the LCM
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
unsigned long long gcd(unsigned long long a, unsigned long long b) {
unsigned long long temp = 0;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
int main(void) {
unsigned long long a, b;
scanf("%llu %llu", &a, &b);
unsigned long long UCLN = gcd(a,b);
if(a==0||b==0)
printf("N/A");
else if((a/UCLN) <= (ULLONG_MAX/b))
printf("%llu",a/UCLN*b);
else{
printf("N/A");
}
return 0;
}
//dual format telephone number
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex phone10("(\\+84|0)1[0-9]{9}");
string st;
int n;
cin >> n;
getline(cin,st);
while(n--){
getline(cin, st);
if(regex_match(st, phone10))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
//email address
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main(){
regex pattern("([\\w!#$%&'*+/=?^_`{|}~-]+\\.?[\\w!#$%&'*+/=?^_`{|}~-]+)+@(\\w+(\\-\\w+)?(\\.\\w+))+");
int x;
cin >>x;
string line_;
getline(cin,line_);
while(x--){
getline(cin,line_);
if(regex_match(line_,pattern))
cout << "Valid!"<<endl;
else
cout <<"Invalid!"<<endl;
}
return 0;
}
//find IP attack
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main(){
regex pattern("\\d+\\ \\d+\\.\\d+(\\ ((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])){2}\\ [A-Z]+\\ \\d+ [A-Z]+\\ [\\w-+/?=&.: ]+");
int t;
string st;
cin >> t;
getline(cin, st);
int i = 1;
while(t--){
getline(cin, st);
cout << i++ << endl;
sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);
sregex_token_iterator end;
for (; pos!=end; ++pos)
cout << "Found " << pos->str() << endl;
}
return 0;
}
//integer number
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex pattern("[\\+-]?[1-9][0-9]*");
string st;
int n;
cin >> n;
getline(cin,st);
while(n--){
getline(cin, st);
if(regex_match(st, pattern))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
//list email in raw data
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main(){
regex pattern("([\\w_-]+(\\.?[\\w_-]))+@[\\w]+([_\\w]+)?(\\.[\\w]+)+");
int t;
string st;
cin >> t;
getline(cin, st);
int i = 1;
while(t--){
getline(cin, st);
cout << i++ << endl;
sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);
sregex_token_iterator end;
for (; pos!=end; ++pos)
cout << "Found " << pos->str()<< endl;
}
return 0;
}
//list IP in log web
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main(){
regex pattern("((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])");
int t;
string st;
cin >> t;
getline(cin, st);
int i = 1;
while(t--){
getline(cin, st);
cout << i++ << endl;
sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);
sregex_token_iterator end;
for (; pos!=end; ++pos)
cout << "Found " << pos->str()<< endl;
}
return 0;
}
//list link in a tag HTML
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main(){
regex pattern("(http|https|ftp)://[\\w\\.]+([-\\w\\.]+)?(:\\d+)?([/\\w]+)?");
int t;
string st;
cin >> t;
getline(cin, st);
int i = 1;
while(t--){
getline(cin, st);
cout << i++ << endl;
sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);
sregex_token_iterator end;
for ( ; pos!=end ; ++pos )
cout << "Found: " << pos->str()<< endl;
}
return 0;
}
//list phone number
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex pattern("((\\+84)|0)[1-9][0-9]{8,9}");
string st;
int n;
cin >> n;
getline(cin, st);
for(int i=1;i<=n;i++){
cout << i << endl;
getline(cin, st);
sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);
sregex_token_iterator end;
for ( ; pos!=end ; ++pos ) {
cout << "Found " << pos->str() << " at " << pos->first-st.begin() << endl;
}
}
return 0;
}
//matching specific FileName
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main(){
regex images("[a-zA-Z0-9\\_\\.]+\\.?[a-zA-Z0-9\\_]+\\.(([jJ][pP][gG])|([pP][nN][gG])|([gG][iI][fF]))");
regex docs("[a-zA-Z0-9\\_\\.]+\\.?[a-zA-Z0-9\\_]+\\.(([pP][dD][f|])|([dD][oO][cC])|([pP]{2}[tT][xX]))");
regex media("[a-zA-Z0-9\\_\\.]+\\.?[a-zA-Z0-9\\_]+\\.(([m|M][p|P][3|4])|([a|A][v|V][i|I]))");
int t;
string st;
cin >> t;
getline(cin, st);
int i = 1;
while(t--){
getline(cin, st);
cout << i++ << endl;
sregex_token_iterator pos(st.cbegin(),st.cend(), images, 0);
sregex_token_iterator end;
for ( ; pos!=end ; ++pos )
cout << pos->str() << " : " << "hinh anh" << endl;
sregex_token_iterator pos1(st.cbegin(),st.cend(), media, 0);
sregex_token_iterator end1;
for ( ; pos1!=end1 ; ++pos1 )
cout << pos1->str() << " : " << "media" << endl;
sregex_token_iterator pos2(st.cbegin(),st.cend(), docs, 0);
sregex_token_iterator end2;
for ( ; pos2!=end2 ; ++pos2 )
cout << pos2->str() << " : " << "tai lieu" << endl;
}
return 0;
}
//modify telephone number
#include <iostream>
#include <regex>
#include <string>
using namespace std;
string Convert(string str){
string temp = "";
string sub = str.substr(0,4);
if(sub.compare("0123") ==0){
regex at_sign("0123");
string replace_by("083");
temp = regex_replace(str, at_sign, replace_by);
}else if(sub.compare("0124") ==0){
regex at_sign("0124");
string replace_by("084");
temp = regex_replace(str, at_sign, replace_by);
}else if(sub.compare("0125") ==0){
regex at_sign("0125");
string replace_by("085");
temp = regex_replace(str, at_sign, replace_by);
}
else if(sub.compare("0127") ==0){
regex at_sign("0127");
string replace_by("081");
temp = regex_replace(str, at_sign, replace_by);
}
else if(sub.compare("0129") ==0){
regex at_sign("0129");
string replace_by("082");
temp = regex_replace(str, at_sign, replace_by);
}
return temp;
}
int main(){
regex pattern("012[3-9^6]\\d{7}");
int t;
string st;
cin >> t;
getline(cin, st);
for(int i = 1; i <= t ; i++){
getline(cin, st);
cout << i << endl;
sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);
sregex_token_iterator end;
for (; pos!=end; ++pos){
string str1 = pos->str();
cout << "Thay doi: " << Convert(str1) << endl;
}
}
return 0;
}
//numeric string leading without zero
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex pattern("[1-9][0-9]*");
string st;
int n;
cin >> n;
getline(cin,st);
while(n--){
getline(cin, st);
if(regex_match(st, pattern))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
//numeric string
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex pattern("[0-9]*");
string st;
int n;
cin >> n;
getline(cin,st);
while(n--){
getline(cin, st);
if(regex_match(st, pattern))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
//telephone number international format
#include<iostream>
#include<regex>
#include <string>
#include <stdexcept>
using namespace std;
int main()
{
regex phone10("\\+841(\\.[0-9]{3}){3}");
string st;
int n;
cin >> n;
getline(cin,st);
while(n--){
getline(cin, st);
if(regex_match(st, phone10))
cout << "Valid!" << endl;
else
cout << "Invalid!" << endl;
}
return 0;
}
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression