28.检查全部或无

说明

有时你想要搜索的模式可能有部分可能存在或可能不存在。尽管如此,检查它们仍然很必要。

你可以使用问号?指定可能存在的元素。这检查前一个元素的零或一。你可以认为这个符号是说前一个元素是可选的。

例如,美英英文略有差异,你可以使用问号来匹配这两个词的拼写。

var american = "color";    
var british = "colour";    
var rainbowRegex= /colou?r/;    
rainbowRegex.test(american); // 返回 true    
rainbowRegex.test(british); // 返回 true

练习

更改正则表达式favRegex以匹配favoritefavourite两个单词。

  • 你的正则表达式应使用可选符号:?
  • 你的正则表达式应该匹配"favorite"
  • 你的正则表达式应该匹配"favourite"
  • 你的正则表达式应该不匹配"fav"
var favRegex = /change/; // Change this line

答案

方法描述
正则表达式正则表达式(英语:Regular Expression,在代码中常简写为regex、regexp或RE)使用单个字符串来描述、匹配一系列符合某个句法规则的字符串搜索模式。
元字符描述
?匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等价于 {0,1}。
var favRegex = /favou?rite/; // Change this line

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个实现以上功能的C++程序: ```c++ #include <iostream> #include <fstream> #include <string> #include <algorithm> using namespace std; struct Employee { int id; string name; int age; double salary; }; bool cmp(Employee a, Employee b) { return a.id < b.id; } void saveToFile(Employee employees[], int n, string filename) { ofstream ofs(filename, ios::out); if (!ofs) { cerr << "Failed to open file for writing!" << endl; return; } sort(employees, employees + n, cmp); for (int i = 0; i < n; i++) { ofs << employees[i].id << " " << employees[i].name << " " << employees[i].age << " " << employees[i].salary << endl; } ofs.close(); } Employee readFromKeyboard() { Employee employee; cout << "Input id, name, age, salary: "; cin >> employee.id >> employee.name >> employee.age >> employee.salary; return employee; } void appendToFile(Employee employee, string filename) { ofstream ofs(filename, ios::app); if (!ofs) { cerr << "Failed to open file for appending!" << endl; return; } ofs << employee.id << " " << employee.name << " " << employee.age << " " << employee.salary << endl; ofs.close(); } void displayAll(string filename) { ifstream ifs(filename, ios::in); if (!ifs) { cerr << "Failed to open file for reading!" << endl; return; } int count = 0; Employee employee; while (ifs >> employee.id >> employee.name >> employee.age >> employee.salary) { cout << employee.id << " " << employee.name << " " << employee.age << " " << employee.salary << endl; count++; } ifs.close(); if (count == 0) { cout << "No employees found!" << endl; } } void searchById(string filename) { ifstream ifs(filename, ios::in); if (!ifs) { cerr << "Failed to open file for reading!" << endl; return; } int id; bool found = false; Employee employee; cout << "Input id to search (0 to exit): "; cin >> id; while (id != 0) { int count = 0; found = false; ifs.seekg(0, ios::beg); while (ifs >> employee.id >> employee.name >> employee.age >> employee.salary) { count++; if (employee.id == id) { found = true; cout << "Employee #" << count << ": " << endl << "id: " << employee.id << " " << "name: " << employee.name << " " << "age: " << employee.age << " " << "salary: " << employee.salary << endl; break; } } if (!found) { cout << "No employee with id " << id << " found!" << endl; } cout << "Input id to search (0 to exit): "; cin >> id; } ifs.close(); } int main() { const int MAX_EMPLOYEES = 5; string filename = "employees.txt"; Employee employees[MAX_EMPLOYEES] = { {103, "Tom", 30, 5000.0}, {101, "Jerry", 28, 4500.0}, {104, "Alice", 35, 8000.0}, {105, "Bob", 32, 6000.0}, {102, "Marry", 26, 4000.0} }; int n = MAX_EMPLOYEES; saveToFile(employees, n, filename); cout << "Saved " << n << " employees to file " << filename << endl; Employee employee1 = readFromKeyboard(); appendToFile(employee1, filename); cout << "Appended 1 employee to file " << filename << endl; Employee employee2 = readFromKeyboard(); appendToFile(employee2, filename); cout << "Appended 1 employee to file " << filename << endl; displayAll(filename); searchById(filename); return 0; } ``` 在这个程序中,我们定义了一个结构体Employee,包含职工号码、姓名、年龄和工资等信息。程序中使用了STL库中的ofstream、ifstream、sort等函数来实现文件的读写和数据的排序等操作。 程序中首先将5个员工的数据保存到文件中,然后从键盘输入两个员工的数据,并将它们追加到文件末尾。接着输出文件中全部职工的数据,最后实现了按职工号从文件中查找职工的功能。 注意,在处理文件时,我们需要对文件是否成功打开进行检查,避免文件操作失败导致程序崩溃。此外,我们在读写文件时使用了文件流的seekg函数来实现文件的随机读取,这样可以快速定位到文件中指定位置的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值