n4gys4nyi
n4gys4nyi

Reputation: 933

java split string with regex and store it in variables

I have a file and i have to read it line by line. The file contains similar lines, like these:

asd@lol kek|1.1.1.1 title@content m e s s a g e|2.3.4.5

I read these lines to String variables. How can i split these strings to other variables? For example: var1=title, var2=content, var3=m e s s a g e, var4=2.3.4.5

I tried something like this, but i cant find the solution:

stringArray=line.split("|");

Upvotes: 1

Views: 690

Answers (1)

anubhava
anubhava

Reputation: 785611

You can use this regex for matching and grab the captured groups:

^([^@]+)@(\S+)\s([^|]+)\|(.*)$

RegEx Demo

I suggest you read up a bit on difference between split and match operations.

Upvotes: 3

Related Questions