user1077071
user1077071

Reputation: 961

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.

Upvotes: 0

Views: 605

Answers (2)

Keith Pinson
Keith Pinson

Reputation: 1725

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.

Upvotes: 0

hwnd
hwnd

Reputation: 70722

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)(.*)

Upvotes: 1

Related Questions