How to break this line into a regular expression?

I have a line, they look like this:

div#title.title.top #main.main a.bold#empty.red 

They are like haml, and I want to break them down by regular expression, but I don't know how to define it.

 val r = """???""".r // HELP val items = "a.bold#empty.red".split(r) items // -> "a", ".bold", "#empty", ".red" 

How to do it?


UPDATE

Sorry, everyone, but I need to make this question more difficult. I am very interested in

 val r = """(?<=\w)\b""" 

But he could not make out the more complex ones:

 div#question-title.title-1.h-222_333 

Hope it will be processed:

 div #question-title .title-1 .h-222_333 

I want to know how to improve this regex?

+6
split scala regex
source share
3 answers

I'm not quite sure what you need here, but this should help:

 (?:\.|#)?\w+ 

This means that a β€œterm” is defined as an optional dot or hash followed by some words.

As a result, you will receive:

 div #title .title .top #main .main a .bold #empty .red 
+2
source share
 val r = """(?<=\w)\b(?!-)""" 

Note that split takes a String value that represents the regular expression, not Regex , so you should not convert r from String to Regex .

A brief explanation of the regular expression:

  • (?<=...) is the appearance. He claims that this match must be preceded by a pattern ... , or in your case \w , which means that the pattern must follow a number, letter or underscore.

  • \b means the word boundary. This is a zero-length match that occurs between a word character (numbers, letters, and underscores) and a character other than a word, or vice versa. Since it is zero length, split will not remove the character when splitting.

  • (?!...) is negative. Here I say that I am not interested in word boundaries from letter to dash.

+8
source share

Starting with Josh M's answer, it has a good regex, but since split accepts a regex matching the "delimiter", you need to use findAllIn as follows:

 val r = """(?:\.|#)?\w+""".r val items = r findAllIn "a.bold#empty.red" //maybe you want a toList on the end also 

Then you will get the results

 div#title.title.top -> List(div, #title, .title, .top) #main.main -> List(#main, .main) a.bold#empty.red -> List(a, .bold, #empty, .red) 
+3
source share

All Articles