sabraship
sabraship

Reputation: 99

Regex Expression to split by "(letter or number)"

Sorry, this question might have an answer already, but I can't seem to find it. I'm trying to split the following string:

Heading:
(A) point A
   (1) subpoint 1
   (2) subpoint 2
(B) point B

into:

[Heading:, point A, subpoint 1, subpoint 2, ... ]

My current code is this:

section.split("\n[\\(\\D\\)]");

Any help would be appreciated! (Also, side question, would it be possible to keep the bullet in the split array? So that the array would have "(A) point A" instead of just "point A"?)

Upvotes: 0

Views: 73

Answers (2)

41686d6564
41686d6564

Reputation: 19641

You may use the following pattern:

\s*\([0-9a-zA-Z]\)\s*

Demo.

Breakdown:

\s*         # Zero or more whitespace characters (including line breaks).
\(          # Matches '(' literally.
[0-9a-zA-Z] # Matches an English letter or digit.
\)          # Matches ')' literally.
\s*         # Zeror or more whitespace characters.

Note: If you want to match more than one letter or number, you may add a + after the character class (i.e., [0-9a-zA-Z]+).

Java example:

String pattern = "\\s*\\([0-9a-zA-Z]\\)\\s*";
String section = "Heading:\n"
     + "(A) point A\n"
     + "   (1) subpoint 1\n"
     + "   (2) subpoint 2\n"
     + "(B) point B";

String[] parts = section.split(pattern);
System.out.println(Arrays.toString(parts));

Output:

[Heading:, point A, subpoint 1, subpoint 2, point B]

Try it online.

Upvotes: 1

duckboycool
duckboycool

Reputation: 2455

A regex of \n\s*\(.\)\s? returns

['Heading:', 'point A', 'subpoint 1', 'subpoint 2', 'point B']

and \n\s* returns

['Heading:', '(A) point A', '(1) subpoint 1', '(2) subpoint 2', '(B) point B']

Upvotes: 1

Related Questions