1

I want it to match each of the text and flexible enough to read multiple lines instead of a single line.

Text single input example

383 Bukit Timah Road #01-09B Alocassia Apartments Singapore 259727

Text with multiple lines example

383 Bukit Timah Road 
#01-09B Alocassia Apartments 
Singapore 259727

448 Ang Mo Kio 
Avenue 10 #01-1693 
Singapore 560448

383 Bukit Timah Road 
#01-09B Alocassia Apartments 
Singapore 259727

Regex:

(\d+ .*\d{6}\b)
2
  • What language/tool are you using? From the regex tag info: "Since regular expressions are not fully standardized, all questions with this tag should also include a tag specifying the applicable programming language or tool." Commented Mar 22, 2020 at 19:44
  • im sorry, i've edited my post Commented Mar 22, 2020 at 19:45

2 Answers 2

1

Use [^] (specific to JavaScript Regex) to match any character including linebreaks

The regex is:

^(\d+ [^]*?\b\d{6})$

Demo & explanation

var test = [
`383 Bukit Timah Road #01-09B Alocassia Apartments Singapore 259727`,
`383 Bukit Timah Road 
#01-09B Alocassia Apartments 
Singapore 259727`,
`448 Ang Mo Kio 
Avenue 10 #01-1693 
Singapore 560448`,
`383 Bukit Timah Road 
#01-09B Alocassia Apartments 
Singapore 259727`,
`123 blah blah`,
`blah blah 123456`,
`123 blah blah 1234567`
];
console.log(test.map(function (a) {
  return a + ' :' + /^(\d+ [^]*?\b\d{6})$/.test(a);
}));

Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect! Thank you so much. I will give this answer to you instead
1

What you're looking for is the s flag (single line: dot matches newline). https://regex101.com/r/ZTP1hC/1

3 Comments

what if i have multiple addresses? it seems to be matching multiple addresses under a single match
I believe does not necessarily have to be but flexible enough to read with/without the new lines
if you want to match multiple instances (addresses in this case) make sure to match as little as possible between the numbers using the ? after the *. So /(\d+ .*?\d{6}\b)/gs

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.