简体   繁体   中英

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 message|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=message, var4=2.3.4.5

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

stringArray=line.split("|");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM