yozawiratama
yozawiratama

Reputation: 4318

Regex get every string from start until new line?

I have a string like this :

Name: Yoza Jr
Address: Street 123, Canada
Email: [email protected]

I need get data using regex until new line, for example

Start with Name: get Yoza Jr until new line for name data

so I can have 3 data Name, Address, Email

How to Regex get every string from start until new line?

btw I will use it in golang : https://regex-golang.appspot.com/assets/html/index.html

Upvotes: 0

Views: 289

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521279

The pattern ^.*$ should work, see the demo here. This assumes that .* would not be running in dot all mode, meaning that .* will not extend past the \r?\n newline at the end of each line.

If you want to capture the field value, then use:

^[^:]+:\s*(\S+)$

The quantity you want will be present in the first capture group.

Upvotes: 2

Anuj Pancholi
Anuj Pancholi

Reputation: 1203

I would suggest you use the pattern ^(.+):\s*(.*)$

Demo: https://regex101.com/r/Q9D4RM/1

Not only will it result in 3 distinct matches for the string given by you, the field name (before the ":") will be read as group 1 of the match, and the value (after the ":") will be read as group 2. So, if you want the key-value pairs, you can just search for groups 1 and 2 for each match.

Please let me know if it's unclear so I can elaborate.

Upvotes: 0

Related Questions