1

iam not able to split string using regex the following code iam using

String[] splitedEmail=email.split(/\\n\;\,\s/);

it's not splitting it's giving the same

3
  • Can you give an example value for email? Commented Apr 24, 2014 at 11:35
  • email="[email protected],[email protected]" Commented Apr 25, 2014 at 14:05
  • Right, so my answer should work? Commented Apr 25, 2014 at 14:16

2 Answers 2

5

You probably want to wrap it in a character class []:

String[] splitedEmail = email.split( /[\n;,\s]/ )
Sign up to request clarification or add additional context in comments.

Comments

-1

Groovy Split() with Regular Expression

Groovy String's split() method can also take a regular expression as a delimiter. Here are some examples:

Split by a single digit:

def sampleText = "A1B23C456D"
println sampleText.split(/\d/)

Since extra occurance of delimiter will yield empty Strings, the code will render:

[A, B, , C, , , D]

Split by 2 digits:

def sampleText = "A1B23C456D"
println sampleText.split(/\d\d/)

Since 23 and 45 are the only consecutive two digits, we will have the result:

[A1B, C, 6D]

Split by any number of digits:

def sampleText = "A1B23C456D"
println sampleText.split(/\d+/)

Will split the String with any length of digit sequence, hence the expected output:

[A, B, C, D]

More details please refer here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.