regex - 创建与另一个部分匹配的新字符串变量
问题描述
我正在使用 Stata 15,我想根据另一个的内容创建一个新的字符串变量。
考虑以下玩具变量:
clear
input str18 string
"a b c"
"d e f"
"g h i"
end
我知道我可以使用该regexm()
函数来提取所有出现的a
、b
和:d
g
generate new = regexm(string, "a|c|d|g")
list
|string new |
|--------------|
| a b c 1 |
| d e f 1 |
| g h i 1 |
但是,我怎样才能得到以下内容?
|string new |
|----------------|
| a b c a c |
| d e f d |
| g h i g |
解决方案
您可以使用该ustrregexra()
函数来消除匹配字符的任何出现:
clear
input str5 string
"a b c"
"d e f"
"g h i"
end
generate wanted = ustrregexra(string, "[^a|c|d|g]", " ")
list
+-----------------+
| string wanted |
|-----------------|
1. | a b c a c |
2. | d e f d |
3. | g h i g |
+-----------------+
如果要消除剩余空间:
replace wanted = strtrim(stritrim(wanted))
+-----------------+
| string wanted |
|-----------------|
1. | a b c a c |
2. | d e f d |
3. | g h i g |
+-----------------+
推荐阅读
- node.js - 为什么秘密管理工具 SecretHub 可以在本地工作,但不能在 heroku 生产中工作?
- mysql - 暂时锁定 SQLite/MySQL 数据库
- python - TypeError:无法将数组数据从 dtype('float64') 转换为 dtype('
我正在尝试运行此代码以进行分析。
import numpy as np import pandas as pd import xarray as xr import matplotlib.pyplot as plt GPCC
- javascript - HTML 按钮重定向
- django - 当我使用 django-ckeditor 没有错误时,如何在 Django 模板上应用多个过滤器?
- mysql - 高效 MySQL 数据库的最佳方法是什么
- python - 如何避免在python类中重复加载数据
- vb.net - 从页面源中提取信息
- r - 根据来自其他 2 个数据框的值创建新列
- r - 如何循环 gls() 函数,以便在一组自变量上回归数据框的每一列