代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

Python正则 Python

提取邮箱地址

import re

text = "Contact us: support@example.com, info@example.org"
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z]{2,}'

emails = re.findall(email_pattern, text)
print(emails)  # ['support@example.com', 'info@example.org']
C++线程 C++

std::thread使用

#include <iostream>
#include <thread>
#include <vector>
using namespace std;

void thread_function(int id) {
    cout << "Thread " << id << " is running\n";
}

int main() {
    vector<thread> threads;
    
    for (int i = 0; i < 5; ++i) {
        threads.push_back(thread(thread_function, i));
    }
    
    for (auto& t : threads) {
        t.join();
    }
    
    cout << "All threads normally terminated\n";
    return 0;
}
MySQL全文搜索 SQL

实现文本搜索

-- Create fulltext index
CREATE FULLTEXT INDEX ftx_index ON articles (title, body);

-- Search using fulltext
SELECT * FROM articles 
WHERE MATCH(title, body) AGAINST('(database  mysql)' IN NATURAL LANGUAGE MODE);
TS类 TypeScript

TypeScript类示例

class Person {
  private name: string;
  age: number;
  
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
  
  describe(): string {
    return `${this.name} is ${this.age} years old`;
  }
}

const alice = new Person('Alice', 30);
console.log(alice.describe());
Python上下文 Python

with语句示例

class FileManager:
    def __init__(self, filename, mode):
        self.file = open(filename, mode)
    
    def __enter__(self, *exc):
        return self.file
    
    def __exit__(self, exc_type, exc_val, traceback):
        self.file.close()

with FileManager('data.txt', 'w') as f:
    f.write('Hello, Context Manager')
SQL视图 SQL

创建用户视图

CREATE VIEW user_summary AS
SELECT
  u.id,
  u.username,
  COUNT(o.id) AS order_count,
  SUM(o.total) AS total_spent
FROM users AS u
JOIN orders AS o ON u.id = o.user_id
GROUP BY u.id;

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类