我需要从该假脱机文件中检索现场用户
(********* 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个结果,我只需要一个,第一个
我该如何解决这个问题?
答案 0 :(得分:0)
该表达式可能不是最快的表达式,但是它会在第二个捕获组中返回所需的输出:
(.*)USER = ([^\s]+)([\s\S]*)
如果需要,可以为其添加更多边界,例如字符列表,而不是([^\s]+)
或start and end chars:
^(.*)USER = ([^\s]+)([\s\S]*)$
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);
您还可以在jex.im中可视化您的表达式: