简体   繁体   中英

Scala Regular Expression

I am having some trouble getting this regular expression just right:

Sample string looks something like this:

"li3001easdasfsaasfasdi5ei1409529297ei1409529597ed16:acl_dec_tag_listl15:DEFAULT_CASE_11e18:avc_app_name_statsd29:Generic Search Engine Trafficd4:sizei5875e5:totali5ee16:Odnoklassniki.Rud4:sizei456e5:totali1ee7:Unknownd4:sizei6391e5:totali2ee5:Yahood4:sizei15673e5:totali1ee10:Yahoo Maild4:sizei5982e5:totali1e"

I want the string to be grouped like this:

(li<digit 1-4>e <string of varying length> i<single digit>e) (<string2 of varying length>)

This is my attempt at this regex so far: (li\\d{1,}e.*i\\de)(.*)

I would like only the first occurrence of li<digits 1-4>e as well.

Simple mistake. * is a greedy operator, meaning it will match as much as it can and still allow the remainder of the regex to match. Use *? instead for a non-greedy match meaning "zero or more — preferably as few as possible".

(li\d{1,}e.*?i\de)(.*)
val s = "li3001easdasfsaasfasdi5easdasfsafas"
val p = """li(\d{1,4})e([^i]*)i(\d)(.*)""".r
val p(first,second,third,fourth) = s

results in:

first: String = 3001
second: String = asdasfsaasfasd
third: String = 5
fourth: String = easdasfsafas

Not sure if that answers your question, but hope it helps.

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