I have a file containing several lines similar to:
Name: Peter
Address: St. Serrano número 12, España
Country: Spain
And I need to extract the address using a regular expression, taking into account that it can contain dots, special characters (ñ, ç), áéíóú...
The current code works, but it looks quite ugly:.
Pattern p = Pattern.compile("^(.+?)Address: ([a-zA-Z0-9ñÑçÇáéíóú., ]+)(.+?)$",
Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = p.matcher(content);
if (m.matches()) { ... }
Edit: The Address field could also be divided into multiple lines
Name: Peter
Address: St. Serrano número 12,
Madrid
España
Country: Spain
Edit: I can't use a Properties object or a YAML parser, as the file contains other kind of information, too.
I don't know Java's regex objects that well, but something like this pattern will do it:
^Address:\s*((?:(?!^\w+:).)+)$
assuming multiline and dotall modes are on.
This will match any line starting with Address, followed by anything until a newline character and a single word followed by a colon.
If you know the next field has to be "Country", you can simplify this a little bit:
^Address:\s*((?:(?!^Country:).)+)$
The trick is in the lookahead assertion in the repeating group. '(?!Country:).' will match everything except the start of the string 'Country:', so we just stick it in noncapturing parentheses (?:...) and quantify it with +, then group all of that in normal capturing parentheses.
You might want to look into Properties
class instead of regex. It provides you ways to manage plain text or XML files to represent key-value pairs.
So you can read in your example file and then get the values like so after loading to a Properties
object:
Properties properties = new Properties();
properties.load(/* InputStream of your file */);
Assert.assertEquals("Peter", properties.getProperty("Name"));
Assert.assertEquals("St. Serrano número 12, España", properties.getProperty("Address"));
Assert.assertEquals("Spain", properties.getProperty("Country"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With