正则表达式删除字符串的前导部分 [英] Regular expression to remove the leading part of a string

查看:114
本文介绍了正则表达式删除字符串的前导部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]+//; 
      

      这篇关于正则表达式删除字符串的前导部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆