使用正则表达式从假脱机文件中检索用户

时间:2019-05-16 17:43:33

标签: regex file find regex-greedy spool

我需要从该假脱机文件中检索现场用户

(********* TOP SEPARATOR ***************************************\
*****************) 48 Tj
0 -1.1 Td
(*        REPORT = COURR QRCODE MOB                             \
 USER = BINM3   *) 48 Tj
0 -2.2 Td
(*        DESC.  = COURRIER E-CLIENT QR CODE MOBILE             \
 FORM = 72A2    *) 48 Tj
0 -3.3 Td
(*        CAPTURE DATE  = 15/05/19 \(19.135\)   TIME = 14.30.45 \
       \(VTUB001 \) *) 48 Tj
0 -4.4 Td
(************************************                           \

TEST
FILE
TEST
FILE

(********* END SEPARATOR ***************************************\
*****************) 48 Tj
0 -1.1 Td
(*        REPORT = COURR QRCODE MOB                             \
 USER = BINM3   *) 48 Tj
0 -2.2 Td
(*        DESC.  = COURRIER E-CLIENT QR CODE MOBILE             \
 FORM = 72A2    *) 48 Tj
0 -3.3 Td
(*        CAPTURE DATE  = 15/05/19 \(19.135\)   TIME = 14.30.45 \
       \(VTUB001 \) *) 48 Tj
0 -4.4 Td
(************************************     

                  \

我用这个

(?<=USER = ).*?(?=\s)

但是我有2个结果,我只需要一个,第一个

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

该表达式可能不是最快的表达式,但是它会在第二个捕获组中返回所需的输出:

(.*)USER = ([^\s]+)([\s\S]*)

如果需要,可以为其添加更多边界,例如字符列表,而不是([^\s]+)start and end chars

^(.*)USER = ([^\s]+)([\s\S]*)$

enter image description here

Demo

JavaScript演示

const regex = /(.*)USER = ([^\s]+)([\s\S]*)/gm;
const str = `(********* TOP SEPARATOR ***************************************\\ *****************) 48 Tj 0 -1.1 Td (* REPORT = COURR QRCODE MOB \\ USER = BINM3 ) 48 Tj 0 -2.2 Td ( DESC. = COURRIER E-CLIENT QR CODE MOBILE \\ FORM = 72A2 ) 48 Tj 0 -3.3 Td ( CAPTURE DATE = 15/05/19 (19.135) TIME = 14.30.45 \\ (VTUB001 ) *) 48 Tj 0 -4.4 Td (************************************ \\

TEST FILE TEST FILE

(********* END SEPARATOR ***************************************\\ *****************) 48 Tj 0 -1.1 Td (* REPORT = COURR QRCODE MOB \\ USER = BINM3 ) 48 Tj 0 -2.2 Td ( DESC. = COURRIER E-CLIENT QR CODE MOBILE \\ FORM = 72A2 ) 48 Tj 0 -3.3 Td ( CAPTURE DATE = 15/05/19 (19.135) TIME = 14.30.45 \\ (VTUB001 ) *) 48 Tj 0 -4.4 Td (************************************ \\`;
const subst = `$2`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

RegEx电路

您还可以在jex.im中可视化您的表达式:

enter image description here