简体   繁体   中英

regex with optional end of input

Should be an easy one but I simply can't figure it out ...

Input will be like

Starting text: 1 x A, 2 x B, 3 x C\\noptionally more text

The "\\n" only comes with the optional text and A, B, C can be anything of any length including dots and hyphens.

Result should be

Starting text: 1 x A, 2 x B, 3 x C

My regex is

Starting text:( \d x .*?[,|\n|\$])+.*?

But that only works if the optional text with "\\n" is present. Otherwise it returns

Starting text: 1 x A, 2 x B,

How to get the "\\$" working?

^(Starting\stext:[\d\w, ]+).*$

description

^         start of line
(         open subpattern
Starting  look for the word "Starting"
\s        look for one whitespace
text      look for the word "text"
:         look for doubledot
[\d\w,\s] look for numbers letters , or whitespace
+         one or more
)         close subpattern
.*        any characters
$         end of line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM