Elixir标点符号替换正则表达式

bluesky1年前 ⋅ 190 阅读

ChatGPT的回答仅作参考:

以下是一些常见的 Elixir 标点符号替换正则表达式:

1. 替换所有逗号为分号:

```
String.replace_all("hello, world!", ~r/,/, ";")
```

输出:`"hello; world!"`

2. 替换所有句号为感叹号:

```
String.replace_all("Hello, world.", ~r/\./, "!")
```

输出:`"Hello, world!"`

3. 替换所有问号为句号:

```
String.replace_all("What's your name?", ~r/\?/, ".")
```

输出:`"What's your name."`

4. 替换所有冒号为破折号:

```
String.replace_all("This is a test: hello world.", ~r/:/, "—")
```

输出:`"This is a test— hello world."`

5. 替换所有括号为方括号:

```
String.replace_all("Hello (world)!", ~r/[()]/, "[]")
```

输出:`"Hello [world]!"`

6. 替换所有引号为单引号:

```
String.replace_all("Hello \"world\"!", ~r/"/, "'")
```

输出:`"Hello 'world'!"`

全部评论: 0

    相关推荐