我想在字符串的两个特定单词之间获取文本 这是我目前的代码:
$test = "13,216 Starting at: $2.03 USD Clutch Case Counter-Strike: Global Offensive 758 Starting at: $2.55 USD Clutch Case Key Counter-Strike: Global Offensive 1,048 Starting at: $2.55 USD Spectrum 2 Case Key Counter-Strike: Global Offensive 1,028 Starting at: $2.60 USD Spectrum Case Key Counter-Strike: Global Offensive 1,421 Starting at: $7.76 USD AK-47 | Redline (Field-Tested) Counter-Strike: Global Offensive 206,745 Starting at: $0.29 USD Boston 2018 Attending Legends Autograph Capsule Counter-Strike: Global Offensive 461 Starting at: $2.56 USD Glove Case Key Counter-Strike: Global Offensive 323,020 Starting at: $0.30 USD Boston 2018 Returning Challengers Autograph Capsule Counter-Strike: Global Offensive 0 Starting at: $12.01 USD AK-47 | Frontside Misty (Field-Tested) Counter-Strike: Global Offensive 615 Starting at: $2.55 USD Gamma 2 Case Key Counter-Strike: Global Offensive 636 Starting at: $2.57 USD Gamma Case Key Counter-Strike: Global Offensive 1,017 Starting at: $1.45 USD AK-47 | Elite Build (Field-Tested) Counter-Strike: Global Offensive 863 Starting at: $1.77 USD AK-47 | Elite Build (Minimal Wear) Counter-Strike: Global Offensive 233 Starting at: $11.04 USD M4A4 | Desolate Space (Field-Tested) Counter-Strike: Global Offensive 183 Starting at: $39.85 USD AWP | Asiimov (Field-Tested) Counter-Strike: Global Offensive 1,237 Starting at: $2.55 USD Operation Hydra Case Key Counter-Strike: Global Offensive 1,034 Starting at: $1.50 USD AWP | Worm God (Minimal Wear) Counter-Strike: Global Offensive 716 Starting at: $2.21 USD AWP | Phobos (Minimal Wear) Counter-Strike: Global Offensive 102 Starting at: $38.14 USD AWP | Hyper Beast (Minimal Wear) Counter-Strike: Global Offensive 435 Starting at: $10.32 USD AWP | Fever Dream (Minimal Wear) Counter-Strike: Global Offensive 20 Starting at: $14.87 USD USP-S | Cortex (Field-Tested) Counter-Strike: Global Offensive 383 Starting at: $3.17 USD M4A4 | Evil Daimyo (Minimal Wear) Counter-Strike: Global Offensive 45 Starting at: $28.58 USD AWP | Mortis (Field-Tested) Counter-Strike: Global Offensive 240,104 Starting at: $0.05 USD Spectrum 2 Case Counter-Strike: Global Offensive 817 Starting at: $6.00 USD M4A1-S | Decimator (Field-Tested) Counter-Strike: Global Offensive 45 Starting at: $36.38 USD AWP | Mortis (Minimal Wear) Counter-Strike: Global Offensive 73 Starting at: $52.99 USD AK-47 | Bloodsport (Minimal Wear) Counter-Strike: Global Offensive 982 Starting at: $1.41 USD AWP | Worm God (Field-Tested) Counter-Strike: Global Offensive 46,075 Starting at: $0.03 USD AUG | Contractor (Field-Tested) Counter-Strike: Global Offensive 728 Starting at: $1.12 USD AK-47 | Elite Build (Well-Worn) Counter-Strike: Global Offensive 8,412 Starting at: $1.61 USD Operation Hydra Case Counter-Strike: Global Offensive 443 Starting at: $8.67 USD AWP | Fever Dream (Field-Tested) Counter-Strike: Global Offensive 354 Starting at: $3.10 USD USP-S | Cyrex (Minimal Wear) Counter-Strike: Global Offensive 286 Starting at: $2.02 USD USP-S | Cyrex (Field-Tested) Counter-Strike: Global Offensive 253 Starting at: $30.84 USD AWP | Asiimov (Battle-Scarred) Counter-Strike: Global Offensive 247 Starting at: $11.59 USD AWP | Man-o'-war (Minimal Wear) Counter-Strike: Global Offensive 948 Starting at: $2.04 USD Name Tag Counter-Strike: Global Offensive 415 Starting at: $3.70 USD M4A1-S | Leaded Glass (Field-Tested) Counter-Strike: Global Offensive 135 Starting at: $19.20 USD AK-47 | Aquamarine Revenge (Field-Tested) Counter-Strike: Global Offensive 117 Starting at: $33.52 USD AWP | Asiimov (Well-Worn) Counter-Strike: Global Offensive 301 Starting at: $3.53 USD AWP | Sun in Leo (Minimal Wear) Counter-Strike: Global Offensive 249 Starting at: $3.69 USD Glock-18 | Water Elemental (Field-Tested) Counter-Strike: Global Offensive 241 Starting at: $7.50 USD AK-47 | Orbit Mk01 (Minimal Wear) Counter-Strike: Global Offensive 28 Starting at: $54.62 USD AWP | Mortis (Factory New) Counter-Strike: Global Offensive 402 Starting at: $2.49 USD M4A4 | Evil Daimyo (Field-Tested) Counter-Strike: Global Offensive 815 Starting at: $2.04 USD AWP | Worm God (Factory New) Counter-Strike: Global Offensive 430 Starting at: $2.11 USD AWP | Phobos (Field-Tested) Counter-Strike: Global Offensive 1,505 Starting at: $0.99 USD AWP | Pit Viper (Field-Tested) Counter-Strike: Global Offensive 1,244 Starting at: $10.43 USD Operation Bravo Case Counter-Strike: Global Offensive 774 Starting at: $2.58 USD Operation Phoenix Case Key Counter-Strike: Global Offensive 314 Starting at: $1.95 USD AK-47 | Emerald Pinstripe (Field-Tested) Counter-Strike: Global Offensive 322 Starting at: $6.76 USD AK-47 | Orbit Mk01 (Field-Tested) Counter-Strike: Global Offensive 260 Starting at: $8.32 USD M4A1-S | Hyper Beast (Field-Tested) Counter-Strike: Global Offensive 250 Starting at: $2.23 USD AWP | Sun in Leo (Field-Tested) Counter-Strike: Global Offensive 197 Starting at: $6.56 USD M4A4 | 龍王 (Dragon King) (Field-Tested) Counter-Strike: Global Offensive 0 Starting at: $13.63 USD AWP | Redline (Field-Tested) Counter-Strike: Global Offensive 162 Starting at: $22.46 USD AK-47 | Neon Revolution (Field-Tested) Counter-Strike: Global Offensive 407 Starting at: $2.56 USD Shadow Case Key Counter-Strike: Global Offensive 901 Starting at: $2.56 USD Chroma 2 Case Key Counter-Strike: Global Offensive 9,672 Starting at: $0.80 USD Huntsman Weapon Case Counter-Strike: Global Offensive 243 Starting at: $3.99 USD StatTrak™ AK-47 | Elite Build (Well-Worn) Counter-Strike: Global Offensive 107 Starting at: $3.56 USD Glock-18 | Moonrise (Field-Tested) Counter-Strike: Global Offensive 122 Starting at: $15.33 USD M4A4 | Desolate Space (Minimal Wear) Counter-Strike: Global Offensive 140 Starting at: $9.61 USD M4A4 | Desolate Space (Well-Worn) Counter-Strike: Global Offensive 594 Starting at: $1.33 USD AWP | Pit Viper (Minimal Wear) Counter-Strike: Global Offensive 149 Starting at: $2.61 USD Operation Breakout Case Key Counter-Strike: Global Offensive 270 Starting at: $4.99 USD Glock-18 | Water Elemental (Minimal Wear) Counter-Strike: Global Offensive 244 Starting at: $4.83 USD AWP | Elite Build (Field-Tested) Counter-Strike: Global Offensive 129 Starting at: $15.58 USD M4A1-S | Hyper Beast (Minimal Wear) Counter-Strike: Global Offensive 0 Starting at: $3.16 USD Desert Eagle | Conspiracy (Minimal Wear) Counter-Strike: Global Offensive 347 Starting at: $7.96 USD M4A1-S | Decimator (Minimal Wear) Counter-Strike: Global Offensive 350 Starting at: $1.58 USD USP-S | Guardian (Factory New) Counter-Strike: Global Offensive 124 Starting at: $14.73 USD AK-47 | Point Disarray (Field-Tested) Counter-Strike: Global Offensive 837 Starting at: $0.95 USD M4A1-S | Flashback (Field-Tested) Counter-Strike: Global Offensive 265 Starting at: $1.27 USD USP-S | Blueprint (Field-Tested) Counter-Strike: Global Offensive 272 Starting at: $3.82 USD M4A4 | Griffin (Minimal Wear) Counter-Strike: Global Offensive 232 Starting at: $2.59 USD Operation Wildfire Case Key Counter-Strike: Global Offensive 5,354 Starting at: $0.19 USD AK-47 | Safari Mesh (Field-Tested) Counter-Strike: Global Offensive 455 Starting at: $2.58 USD Falchion Case Key Counter-Strike: Global Offensive 107 Starting at: $29.50 USD AK-47 | Vulcan (Field-Tested) Counter-Strike: Global Offensive 0 Starting at: $18.36 USD AWP | Redline (Minimal Wear) Counter-Strike: Global Offensive 778 Starting at: $1.27 USD Five-SeveN | Monkey Business (Field-Tested) Counter-Strike: Global Offensive 527 Starting at: $1.24 USD AK-47 | Elite Build (Battle-Scarred) Counter-Strike: Global Offensive 433 Starting at: $2.55 USD Chroma Case Key Counter-Strike: Global Offensive 393 Starting at: $4.86 USD StatTrak™ AK-47 | Elite Build (Field-Tested) Counter-Strike: Global Offensive 269 Starting at: $3.50 USD AK-47 | Cartel (Field-Tested) Counter-Strike: Global Offensive 521 Starting at: $2.83 USD AWP | Phobos (Factory New) Counter-Strike: Global Offensive 54 Starting at: $27.53 USD AK-47 | Aquamarine Revenge (Minimal Wear) Counter-Strike: Global Offensive 427 Starting at: $1.23 USD USP-S | Guardian (Minimal Wear) Counter-Strike: Global Offensive 158 Starting at: $26.04 USD AWP | BOOM (Minimal Wear) Counter-Strike: Global Offensive 432 Starting at: $1.66 USD M4A1-S | Flashback (Minimal Wear) Counter-Strike: Global Offensive 459 Starting at: $1.24 USD USP-S | Torque (Factory New) Counter-Strike: Global Offensive 341 Starting at: $1.00 USD USP-S | Torque (Minimal Wear) Counter-Strike: Global Offensive 305 Starting at: $5.03 USD Desert Eagle | Kumicho Dragon (Field-Tested) Counter-Strike: Global Offensive 268 Starting at: $8.30 USD M4A1-S | Cyrex (Minimal Wear) Counter-Strike: Global Offensive 218 Starting at: $4.39 USD M4A4 | Evil Daimyo (Factory New) Counter-Strike: Global Offensive 570 Starting at: $4.02 USD AK-47 | Blue Laminate (Minimal Wear) Counter-Strike: Global Offensive 1,060 Starting at: $0.71 USD Desert Eagle | Oxide Blaze (Factory New) Counter-Strike: Global Offensive 128 Starting at: $9.13 USD M4A4 | 龍王 (Dragon King) (Minimal Wear) Counter-Strike: Global Offensive 140 Starting at: $9.90 USD AK-47 | Orbit Mk01 (Factory New) Counter-Strike: Global Offensive";
$quantity=preg_match_all('/(USD=?) (.*?) Counter-Strike:/i',$test,$matches);
print_r($matches[0]);
输出如下:
[0] => USD Clutch Case
[1] => USD Clutch Case Key Counter-Strike:
[2] => USD Spectrum 2 Case Key Counter-Strike:
[3] => USD Spectrum Case Key Counter-Strike:
[4] => USD AK-47 | Redline (Field-Tested) Counter-Strike:
[5] => USD Boston 2018 Attending Legends Autograph Capsule Counter-Strike:
但输出应如下所示:
[0] => Clutch Case
[1] => Clutch Case Key
[2] => Spectrum 2 Case Key
[3] => Spectrum Case Key
[4] => AK-47 | Redline (Field-Tested)
[5] => Boston 2018 Attending Legends Autograph Capsule
感谢。
答案 0 :(得分:0)
您想要在USD
和Counter-Strike:
之间提取子字符串。示例数据中的目标子字符串似乎不包含:
,因此我将推荐包含:
的否定字符类。使用\K
(全字符串重启)和正向前瞻,可以避免膨胀输出数组,只需访问全字符串匹配[0]
值。您的模式中的可选等号对于您的示例输入似乎完全没有必要,因此应删除=?
。 (也许你试图执行一个lookbehind - 这不是正确的语法,而且\K
无论如何都会避免模式减慢的外观。)
模式:/USD \K[^:]*(?= Counter-Strike:)/
(Pattern Demo)
代码:(Demo)
var_export(preg_match_all('/USD \K[^:]*(?= Counter-Strike:)/i',$test,$out)?$out[0]:[]);
输出:(100场比赛)
array (
0 => 'Clutch Case',
1 => 'Clutch Case Key',
2 => 'Spectrum 2 Case Key',
3 => 'Spectrum Case Key',
4 => 'AK-47 | Redline (Field-Tested)',
5 => 'Boston 2018 Attending Legends Autograph Capsule',
6 => 'Glove Case Key',
7 => 'Boston 2018 Returning Challengers Autograph Capsule',
8 => 'AK-47 | Frontside Misty (Field-Tested)',
9 => 'Gamma 2 Case Key',
10 => 'Gamma Case Key',
11 => 'AK-47 | Elite Build (Field-Tested)',
12 => 'AK-47 | Elite Build (Minimal Wear)',
13 => 'M4A4 | Desolate Space (Field-Tested)',
14 => 'AWP | Asiimov (Field-Tested)',
15 => 'Operation Hydra Case Key',
16 => 'AWP | Worm God (Minimal Wear)',
17 => 'AWP | Phobos (Minimal Wear)',
18 => 'AWP | Hyper Beast (Minimal Wear)',
19 => 'AWP | Fever Dream (Minimal Wear)',
20 => 'USP-S | Cortex (Field-Tested)',
21 => 'M4A4 | Evil Daimyo (Minimal Wear)',
22 => 'AWP | Mortis (Field-Tested)',
23 => 'Spectrum 2 Case',
24 => 'M4A1-S | Decimator (Field-Tested)',
25 => 'AWP | Mortis (Minimal Wear)',
26 => 'AK-47 | Bloodsport (Minimal Wear)',
27 => 'AWP | Worm God (Field-Tested)',
28 => 'AUG | Contractor (Field-Tested)',
29 => 'AK-47 | Elite Build (Well-Worn)',
30 => 'Operation Hydra Case',
31 => 'AWP | Fever Dream (Field-Tested)',
32 => 'USP-S | Cyrex (Minimal Wear)',
33 => 'USP-S | Cyrex (Field-Tested)',
34 => 'AWP | Asiimov (Battle-Scarred)',
35 => 'AWP | Man-o\'-war (Minimal Wear)',
36 => 'Name Tag',
37 => 'M4A1-S | Leaded Glass (Field-Tested)',
38 => 'AK-47 | Aquamarine Revenge (Field-Tested)',
39 => 'AWP | Asiimov (Well-Worn)',
40 => 'AWP | Sun in Leo (Minimal Wear)',
41 => 'Glock-18 | Water Elemental (Field-Tested)',
42 => 'AK-47 | Orbit Mk01 (Minimal Wear)',
43 => 'AWP | Mortis (Factory New)',
44 => 'M4A4 | Evil Daimyo (Field-Tested)',
45 => 'AWP | Worm God (Factory New)',
46 => 'AWP | Phobos (Field-Tested)',
47 => 'AWP | Pit Viper (Field-Tested)',
48 => 'Operation Bravo Case',
49 => 'Operation Phoenix Case Key',
50 => 'AK-47 | Emerald Pinstripe (Field-Tested)',
51 => 'AK-47 | Orbit Mk01 (Field-Tested)',
52 => 'M4A1-S | Hyper Beast (Field-Tested)',
53 => 'AWP | Sun in Leo (Field-Tested)',
54 => 'M4A4 | 龍王 (Dragon King) (Field-Tested)',
55 => 'AWP | Redline (Field-Tested)',
56 => 'AK-47 | Neon Revolution (Field-Tested)',
57 => 'Shadow Case Key',
58 => 'Chroma 2 Case Key',
59 => 'Huntsman Weapon Case',
60 => 'StatTrak™ AK-47 | Elite Build (Well-Worn)',
61 => 'Glock-18 | Moonrise (Field-Tested)',
62 => 'M4A4 | Desolate Space (Minimal Wear)',
63 => 'M4A4 | Desolate Space (Well-Worn)',
64 => 'AWP | Pit Viper (Minimal Wear)',
65 => 'Operation Breakout Case Key',
66 => 'Glock-18 | Water Elemental (Minimal Wear)',
67 => 'AWP | Elite Build (Field-Tested)',
68 => 'M4A1-S | Hyper Beast (Minimal Wear)',
69 => 'Desert Eagle | Conspiracy (Minimal Wear)',
70 => 'M4A1-S | Decimator (Minimal Wear)',
71 => 'USP-S | Guardian (Factory New)',
72 => 'AK-47 | Point Disarray (Field-Tested)',
73 => 'M4A1-S | Flashback (Field-Tested)',
74 => 'USP-S | Blueprint (Field-Tested)',
75 => 'M4A4 | Griffin (Minimal Wear)',
76 => 'Operation Wildfire Case Key',
77 => 'AK-47 | Safari Mesh (Field-Tested)',
78 => 'Falchion Case Key',
79 => 'AK-47 | Vulcan (Field-Tested)',
80 => 'AWP | Redline (Minimal Wear)',
81 => 'Five-SeveN | Monkey Business (Field-Tested)',
82 => 'AK-47 | Elite Build (Battle-Scarred)',
83 => 'Chroma Case Key',
84 => 'StatTrak™ AK-47 | Elite Build (Field-Tested)',
85 => 'AK-47 | Cartel (Field-Tested)',
86 => 'AWP | Phobos (Factory New)',
87 => 'AK-47 | Aquamarine Revenge (Minimal Wear)',
88 => 'USP-S | Guardian (Minimal Wear)',
89 => 'AWP | BOOM (Minimal Wear)',
90 => 'M4A1-S | Flashback (Minimal Wear)',
91 => 'USP-S | Torque (Factory New)',
92 => 'USP-S | Torque (Minimal Wear)',
93 => 'Desert Eagle | Kumicho Dragon (Field-Tested)',
94 => 'M4A1-S | Cyrex (Minimal Wear)',
95 => 'M4A4 | Evil Daimyo (Factory New)',
96 => 'AK-47 | Blue Laminate (Minimal Wear)',
97 => 'Desert Eagle | Oxide Blaze (Factory New)',
98 => 'M4A4 | 龍王 (Dragon King) (Minimal Wear)',
99 => 'AK-47 | Orbit Mk01 (Factory New)',
)
如果您发现您的实际目标子字符串可能包含:
,那么您可以用.*?
替换否定字符类,但这会略微影响效率,因为贪婪模式优于懒惰模式。
对于样本输入,不需要不区分大小写的模式修饰符(i
)。移除它将提高模式效率200步并保持相同的准确度。如果您的实际数据需要不区分大小写,则应保留i
标记;否则放弃它。
P.S。此模式/调用也将起作用:
var_export(preg_match_all('/USD \K([^:]*) Counter-Strike:/i',$test,$out)?$out[1]:[]);
输出数组膨胀的轻微副作用 - $out
的大小会加倍。