使用正则表达式获取两个不同字符之间的字符串

时间:2022-11-27 18:50:21

I'm struggling to find the right syntax for the following string:

我正在努力为以下字符串找到正确的语法:

^Lorem/ipsum dolor sit/amet consectetur adipiscing/elit

Here, I want to extract the word between ^ and / and the first word after the first /. That is: Lorem and ipsum.

在这里,我想提取^和/之间的单词和第一个/之后的第一个单词。那就是:Lorem和ipsum。

Moreover, how do I change the syntax if I want to extract the word after the second /? This would be amet.

此外,如果我想在第二个/之后提取单词,如何更改语法?这将是一个很好的结果。

2 个解决方案

#1


1  

There are two parts:

有两个部分:

  1. First you want to match everything after a ^ and before a /:

    首先,你希望在^之后和/之前匹配所有内容:

    /\^([^\/]*)/

    / \ ^([^ \ /] *)/

    • \^ matches the character ^ literally
    • \ ^字面上匹配字符^
    • 1st Capturing group ([^\/]*)
      • [^\/] match a single character not present in the list below
      • [^ \ /]匹配下面列表中不存在的单个字符
      • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
      • 量词:*在零和无限次之间,尽可能多次,根据需要回馈[贪心]
    • 第一个捕获组([^ \ / * *] [^ \ /]匹配量化器下面列表中不存在的单个字符:*在零和无限次之间,尽可能多次,根据需要返回[贪婪]
    • \/ matches the character / literally
    • \ /匹配字符/字面
  2. Second you want to match every word after a /:

    其次你要匹配/后的每个单词:

    /\/([^\s\/]*)/g

    / \ /([^ \ S \ /] *)/克

    • \/ matches the character / literally
    • \ /匹配字符/字面
    • 1st Capturing group ([^\s\/]*)
      • [^\s\/] match a single character not present in the list below
      • [^ \ s \ /]匹配下面列表中不存在的单个字符
      • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
      • 量词:*在零和无限次之间,尽可能多次,根据需要回馈[贪心]
      • \s match any white space character [\r\n\t\f ]
      • \ s匹配任何空格字符[\ r \ n \ t \ f]
      • \/ matches the character / literally
      • \ /匹配字符/字面
    • 第一个捕获组([^ \ s \ /] *)[^ \ s \ /]匹配Quantifier下面列表中不存在的单个字符:*在零和无限次之间,尽可能多次,根据需要返回[ \ greedy] \ s匹配任何空格字符[\ r \ n \ t \ f] \ /匹配字符/字面意思
    • g modifier: global. All matches (don't return on first match)
    • g修饰符:全局。所有比赛(首场比赛不返回)

Code (PHP):

代码(PHP):

$str = "^Lorem/ipsum dolor sit/amet consectetur adipiscing/elit"; 

// Part one:
preg_match("/\\^([^\\/]*)/", $str, $matches);

$matches[1] == 'Lorem'; // True    

// Part two:
preg_match_all("/\\/([^\\s\\/]*)/", $str, $matches); 

$matches[1] == 'ipsum';
$matches[2] == 'amet';
$matches[3] == 'elit';

Code and explaination from wonderful tool regex101.

精彩工具regex101的代码和解释。

#2


0  

The 2 words are in capture group 1 and 2. Regex101.

这两个单词位于捕获组1和2中.Regex101。

\^(\w+)\/(\w+)

If you want to capture amet, use this:

如果要捕获amet,请使用以下命令:

\^(\w+)\/.+?\/(\w+)

#1


1  

There are two parts:

有两个部分:

  1. First you want to match everything after a ^ and before a /:

    首先,你希望在^之后和/之前匹配所有内容:

    /\^([^\/]*)/

    / \ ^([^ \ /] *)/

    • \^ matches the character ^ literally
    • \ ^字面上匹配字符^
    • 1st Capturing group ([^\/]*)
      • [^\/] match a single character not present in the list below
      • [^ \ /]匹配下面列表中不存在的单个字符
      • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
      • 量词:*在零和无限次之间,尽可能多次,根据需要回馈[贪心]
    • 第一个捕获组([^ \ / * *] [^ \ /]匹配量化器下面列表中不存在的单个字符:*在零和无限次之间,尽可能多次,根据需要返回[贪婪]
    • \/ matches the character / literally
    • \ /匹配字符/字面
  2. Second you want to match every word after a /:

    其次你要匹配/后的每个单词:

    /\/([^\s\/]*)/g

    / \ /([^ \ S \ /] *)/克

    • \/ matches the character / literally
    • \ /匹配字符/字面
    • 1st Capturing group ([^\s\/]*)
      • [^\s\/] match a single character not present in the list below
      • [^ \ s \ /]匹配下面列表中不存在的单个字符
      • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
      • 量词:*在零和无限次之间,尽可能多次,根据需要回馈[贪心]
      • \s match any white space character [\r\n\t\f ]
      • \ s匹配任何空格字符[\ r \ n \ t \ f]
      • \/ matches the character / literally
      • \ /匹配字符/字面
    • 第一个捕获组([^ \ s \ /] *)[^ \ s \ /]匹配Quantifier下面列表中不存在的单个字符:*在零和无限次之间,尽可能多次,根据需要返回[ \ greedy] \ s匹配任何空格字符[\ r \ n \ t \ f] \ /匹配字符/字面意思
    • g modifier: global. All matches (don't return on first match)
    • g修饰符:全局。所有比赛(首场比赛不返回)

Code (PHP):

代码(PHP):

$str = "^Lorem/ipsum dolor sit/amet consectetur adipiscing/elit"; 

// Part one:
preg_match("/\\^([^\\/]*)/", $str, $matches);

$matches[1] == 'Lorem'; // True    

// Part two:
preg_match_all("/\\/([^\\s\\/]*)/", $str, $matches); 

$matches[1] == 'ipsum';
$matches[2] == 'amet';
$matches[3] == 'elit';

Code and explaination from wonderful tool regex101.

精彩工具regex101的代码和解释。

#2


0  

The 2 words are in capture group 1 and 2. Regex101.

这两个单词位于捕获组1和2中.Regex101。

\^(\w+)\/(\w+)

If you want to capture amet, use this:

如果要捕获amet,请使用以下命令:

\^(\w+)\/.+?\/(\w+)