正则表达式删除字符串的前导部分

编程入门 行业动态 更新时间:2024-10-25 18:24:04
本文介绍了正则表达式删除字符串的前导部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有不同的字符串,但格式相同,用空格分隔的三个单词.目标是删除字符串的第一部分.换句话说,删除每个字符串的前导数据.

I have different strings yet formatted the same with three words separated by whitespace. The goal is to delete the first part of the string. In other words, delete the leading data for each string.

什么 Perl 正则表达式能让我删除前导数据,同时保持字符串的其余部分不受影响?

What Perl regular expression will enable me to delete the leading data while keeping the rest of the string unaffected?

String 1: Apples Peaches Grapes String 2: Spinach Tomatoes Carrots String 3: Corn Potatoes Rice

输出

String 1: Peaches Grapes String 2: Tomatoes Carrots String 3: Potatoes Rice

Perl

#! /usr/bin/perl use v5.10.0; use warnings; $string1 = "Apples Peaches Grapes"; $string2 = "Spinach Tomatoes Carrots"; $string3 = "Corn Potatoes Rice"; # Apply ReqExp to Delete the First Part of the String $string1 =~ s/.../; say $string1; say $string2; say $string3;

推荐答案

$string1 =~ s/^\S+\h+//;

  • ^ 字符串开头
  • \S+ 1 个或多个非空格字符
  • \h+ 1 个或多个水平空格
    • ^ beginning of string
    • \S+ 1 or more non space character
    • \h+ 1 or more horizontal spaces
    • 如果您使用的是低于 v5.10 的 Perl 版本,您可以使用:

      If you're using a version of Perl older than v5.10, you may use:

      $string1 =~ s/^\S+[ \t]+//;

更多推荐

正则表达式删除字符串的前导部分

本文发布于:2023-11-03 12:58:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前导   字符串   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!