String 正则去除字符串和

    public static String trimStr(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }

        return str.replaceAll("\\s*|\t|\r|\n","").replaceAll("[\\pP\\p{Punct}]","");
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在STM32中,可以使用正则表达式来提取字符串中的电话号码。以下是一个示例代码: ```c #include <stdio.h> #include <string.h> #include "stm32f4xx_hal.h" #include "stm32f4xx_hal_def.h" #include "stm32f4xx_hal_uart.h" #include "stm32f4xx_hal_gpio.h" #include "stm32f4xx_hal_cortex.h" #include "stm32f4xx_hal_rcc.h" #include "stm32f4xx_hal_dma.h" #include "stm32f4xx_hal_adc.h" #include "stm32f4xx_hal_adc_ex.h" #include "stm32f4xx_hal_tim.h" #include "regex.h" char *phone_number_regex = "\\d{3}-\\d{3}-\\d{4}"; int main(void) { // 初始化串口 UART_HandleTypeDef huart1; huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&huart1); // 初始化正则表达式 regex_t regex; int ret = regcomp(&regex, phone_number_regex, REG_EXTENDED); if (ret != 0) { printf("Failed to compile regex\n"); return -1; } // 读取串口数据 char buffer[100]; while (1) { if (HAL_UART_Receive(&huart1, (uint8_t *)buffer, 100, 1000) == HAL_OK) { buffer[strlen(buffer) - 1] = '\0'; // 去除换行符 regmatch_t match[1]; ret = regexec(&regex, buffer, 1, match, 0); if (ret == 0) { char phone_number[12]; strncpy(phone_number, buffer + match[0].rm_so, match[0].rm_eo - match[0].rm_so); phone_number[match[0].rm_eo - match[0].rm_so] = '\0'; printf("Phone number: %s\n", phone_number); } else { printf("No phone number found\n"); } } } return 0; } ``` 在上面的示例代码中,我们首先通过正则表达式编译函数 `regcomp` 初始化正则表达式。然后在读取串口数据时,使用 `regexec` 函数和正则表达式匹配字符串,如果匹配成功,则提取电话号码并打印出来。否则,打印出未找到电话号码的提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值