掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 792|回复: 4

[求助] 关于文件完整路径的正则提取

[复制链接]

该用户从未签到

发表于 2020-3-16 11:17:05 | 显示全部楼层 |阅读模式
本帖最后由 dongzhi1980 于 2020-3-16 11:21 编辑 7 X. i$ M; I* t) Y' d) u7 f
2 F: Y9 t6 ~! Q
文件内容共4行,存储内容为文件的地址(pdf和.7z格式)和网页链接。
' }& \, y+ z$ X. Y0 w目标是:提取每个PDF文件的全名(形如abc.pdf等)及其前一项的ID(文件名前面一项,形如BUUXZJBF,BZ7QHC8S等8位数的ID标签)。
: X2 [+ T1 x' U" \6 R' P8 U- S# @% m由于,1.文件名中有特殊符号"\;", 2.里面混杂了.7z文件。我在python re 下没有成功提取,请大家出手相助。注:只需提供正则表达式即可。谢谢
& Q; u3 r6 P1 i
, L! p7 U6 N& k' E% j7 M5 lfile = {E\:\\storage\\BUUXZJBF\\Kottwitz - 2011 - Xyz beginner's guide \; one book name .pdf},
6 \, F! }% d3 N; Efile = {E\:\\storage\\BZ7QHC8S\\Xyz programming - an introduction..pdf;E\:\\storage\\I5CN6Q4Y\\ABC Programming Advanced Introduction.7z;E\:\\storage\\LIEKLY4B\\NO programming - an advanced introduction chs..pdf;E\:\\storage\\4UVU3D66\\www.ccc-intro.org.html},
  E5 l, N3 M  \, A; X9 L  ^2 ^5 Ihowpublished = {http://www.123.org/}5 {( F  }# p" i& t( S/ P0 {" m6 ]
file = {E\:\\storage\\CUUXZJBG\\Tomz - 2015 - OK beginner's guide another book name.pdf}

该用户从未签到

发表于 2020-3-16 11:46:42 | 显示全部楼层

0 b; }; h, r; V( c1 M5 A1 w! C3 T* Y% ]8 F0 Y3 \7 N# [: F( T  m0 L
  1. import re  {" \) ?; M0 s7 v5 R1 |! F

  2. 0 u8 ^, V$ s& l$ E1 a
  3. string = """file = {E\:\\storage\\BUUXZJBF\\Kottwitz - 2011 - Xyz beginner's guide \; one book name .pdf},, Y0 Q1 ]9 T8 i
  4. file = {E\:\\storage\\BZ7QHC8S\\Xyz programming - an introduction..pdf;E\:\\storage\\I5CN6Q4Y\\ABC Programming Advanced Introduction.7z;E\:\\storage\\LIEKLY4B\\NO programming - an advanced introduction chs..pdf;E\:\\storage\\4UVU3D66\\www.ccc-intro.org.html},7 i9 F) s& _1 q; {, i
  5. howpublished = {http://www.123.org/}" }% z/ b- K9 e# J2 n
  6. file = {E\:\\storage\\CUUXZJBG\\Tomz - 2015 - OK beginner's guide another book name.pdf}"""2 d( {, D; h% B% W/ J/ Y1 X, R4 e

  7. / ^9 {# {( c4 u4 ~9 B" @
  8. % v9 J* A9 N2 V1 T  |, w1 `, O. b. ^
  9. pattern = re.compile(r"\\([0-8A-Z]{8})\\([^:}]+?\.pdf)")
    6 _" _$ o4 o7 A3 d7 {3 w5 |

  10. $ [" M% F+ y+ v
  11. m = pattern.findall(string)& P) n5 V- z. Z4 c( F

  12. 8 U7 L( d$ }2 P0 G( K( |* v
  13. for item in m:
    + j8 P' s" ~. `& ?4 \! _
  14.     print(item)
复制代码

! t. {$ c9 d8 M  S) U& N7 p+ E" r
输出, i2 Y  G; x. T, S, f4 ]

& v1 B0 D+ H' H; j% {
  1. ('BUUXZJBF', "Kottwitz - 2011 - Xyz beginner's guide \\; one book name .pdf")  Q( x+ K7 _+ }0 D: W  L# o: \# I
  2. ('BZ7QHC8S', 'Xyz programming - an introduction..pdf')! B0 }+ p1 m: U: g, n1 N
  3. ('LIEKLY4B', 'NO programming - an advanced introduction chs..pdf')
    ' P% I9 R" i# H9 x. M* ]
  4. ('CUUXZJBG', "Tomz - 2015 - OK beginner's guide another book name.pdf")
复制代码

: |; M, q+ @) }, \3 q2 k
1 ^) E, |( g: M2 D& h

评分

1

查看全部评分

该用户从未签到

 楼主| 发表于 2020-3-16 17:51:07 | 显示全部楼层
本帖最后由 dongzhi1980 于 2020-3-16 18:17 编辑 % O0 r" O, O+ Z4 a& S4 P
Oeasy 发表于 2020-3-16 11:46/ n! W& y  @8 g! F; v5 u. k
输出

) V% `: s4 v6 n; l' q5 n% D谢谢O大的详细解答,请问,
; e! d; A7 w  g. y, B- x文件名中多余的“\”怎么去除,真实文件名是"Kottwitz - 2011 - Xyz beginner's guide ; one book name .pdf"。& l" _" B6 W5 H4 u% D4 D+ l
生成的字符串 '\\Kottwitz - 2011 - LaTeX beginner\'s guide \\; create high-quality and .pdf' 中
: l0 ]" H9 M" H4 ~4 y
/ A. T1 e8 N. z5 I1 {/ [' }) H多余的"\"导致程序后续操作产生问题。
7 D% L2 ^9 O2 f2 v. k2 y, O4 h, L% x) n1 c. K* d, F! ]
4 j% ]; g- g. @3 ]% I9 @* ]
! w$ k: ]# ?5 ?( r* W3 L

该用户从未签到

发表于 2020-3-16 18:36:51 | 显示全部楼层
dongzhi1980 发表于 2020-3-16 17:51* D1 s" K- V# H9 \
谢谢O大的详细解答,请问,8 a# j% p0 P% `& M9 n* {
文件名中多余的“\”怎么去除,真实文件名是"Kottwitz - 2011 - Xyz beginner' ...
% g; |+ N0 b, g% q9 s" x1 X

; {$ F3 S1 o# M$ z2 b7 Z0 z) b" {: S# G/ x( q& q* _$ [4 `% Y
抱歉手抖吧 0-9 敲成 0-8 了。
6 f; j% X2 h0 G4 o, ^' `4 z  P) t- m2 [5 [1 T
  1. import re; g5 W& Y+ ~( ]# k) U- {: M2 g5 D1 m: `( U
  2. 7 @7 T/ H6 u; g+ G5 k* P  I, F
  3. string = """file = {E\:\\storage\\BUUXZJBF\\Kottwitz - 2011 - Xyz beginner's guide \; one book name .pdf},. f8 |) [' J6 {3 W* Y5 A; @
  4. file = {E\:\\storage\\BZ7QHC8S\\Xyz programming - an introduction..pdf;E\:\\storage\\I5CN6Q4Y\\ABC Programming Advanced Introduction.7z;E\:\\storage\\LIEKLY4B\\NO programming - an advanced introduction chs..pdf;E\:\\storage\\4UVU3D66\\www.ccc-intro.org.html},8 c  X+ x( w; d! W# ^
  5. howpublished = {http://www.123.org/}0 f) m  g3 h7 U, G
  6. file = {E\:\\storage\\CUUXZJBG\\Tomz - 2015 - OK beginner's guide another book name.pdf}# o; b! A+ g, }  _" Y
  7. file = {E\:\\Data\\storage\\2MTGE89K\\Matlab users guide_Symbolic Math Toolbox.pdf;E\:\\Data\\storage\\CS58DYJW\\Matlab users guide_Parallel Computing Toolbox.pdf;E\:\\Data\\storage\\IAR9VKWB\\Matlab Programming Fundamentals.pdf;E\:\\Data\\storage\\RV3RTI3V\\Matlab Mathematics.pdf}"""3 P% M  q' c+ V; E
  8. 2 f5 t9 u0 l7 B" ~8 r' z6 }

  9. / w, L5 Y0 t7 o. C8 }/ q
  10. pattern = re.compile(r"\\([0-9A-Z]{8})\\([^:}]+?\.pdf)")# [) L8 i9 C$ B8 ]; y! v7 I: E
  11. ( R: J4 H* b0 {( G2 b4 D6 q/ ^
  12. m = pattern.findall(string)/ w" u- L, E' w, n) s

  13. 6 O( P1 q' B: z$ U0 W' [
  14. for item in m:
    6 N9 i% L, D2 y7 }/ z. X
  15.     print(item[0], item[1].replace("\",""))
复制代码
0 A& T& D  C9 u* o" E# T2 i
输出
7 c5 E# Y/ l' Q" E6 I# \
( A, G" n" E8 j' h
  1. BUUXZJBF Kottwitz - 2011 - Xyz beginner's guide ; one book name .pdf; }' W1 i- J9 e
  2. BZ7QHC8S Xyz programming - an introduction..pdf, c- A7 a* q. U5 ]7 S& m
  3. LIEKLY4B NO programming - an advanced introduction chs..pdf/ H$ E9 ^1 Y( ]% Z6 [
  4. CUUXZJBG Tomz - 2015 - OK beginner's guide another book name.pdf
    ' d) A- m8 A5 j- L2 \
  5. 2MTGE89K Matlab users guide_Symbolic Math Toolbox.pdf) G5 m. n# c6 z. F; U) p* g- @9 w
  6. CS58DYJW Matlab users guide_Parallel Computing Toolbox.pdf
    0 C% ]8 d+ z. B
  7. IAR9VKWB Matlab Programming Fundamentals.pdf
    $ J! Q* b3 H9 U2 P8 X1 k
  8. RV3RTI3V Matlab Mathematics.pdf
复制代码
' U  q6 `3 j3 p& t/ y
1 ]  J; ^5 t4 M0 Y) U; K5 j' D

该用户从未签到

 楼主| 发表于 2020-3-16 19:36:27 | 显示全部楼层
本帖最后由 dongzhi1980 于 2020-3-16 20:21 编辑 ; H, @1 r' L9 J
Oeasy 发表于 2020-3-16 18:36
9 ~. M' V* ?! b6 e) }抱歉手抖吧 0-9 敲成 0-8 了。

  o, A$ X' z- i& [2 ~再次感谢,0-8 的问题我自己仔细查看后给解决了,正想着“挖坑不填”的含意呢,正好收到回复了。意外的惊喜。
8 b5 i6 p( q; |' x+ M  }: V最后再麻烦你一次,如果不用 “:}” 排除,改为用 “E\:” 和 “}” 排除,用什么方法呢,在网上查了很多关于断言的操作,但还是不会用。
  g( c- D/ d$ a& M$ U谢谢!" b& Z8 u4 r3 A) S1 }
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

小黑屋|手机版|Archiver|PDAWIKI |网站地图

GMT+8, 2024-4-25 18:03 , Processed in 0.079469 second(s), 10 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表