I have a text where I need to capture the second group
Response on your Property Listing
Dear Rahul Bond,
A user is interested in your Property, ID 62455995: 2 BHK , Multistorey
Apartment in Raheja Vihar , Mumbai.
Details of Contact Made:
Sender's Name: Test (Individual)
The text to be retrieved is 2 BHK , Multistorey Apartment in Raheja Vihar , Mumbai. This does start with **A user is interested in your Property, ID xxxxxxx: **
A user is interested in your Property, ID 62455995: 2 BHK , Multistorey
Apartment in Raheja Vihar , Mumbai.
The expected output is 2 BHK , Multistorey Apartment in Raheja Vihar , Mumbai.
I have tried with .match(/(A user is interested in your Property, ID.*[^:]) (.*\n.*)/g)
Unfortunetly it gives me all 2 lined text.
CodePudding user response:
You can use this regex:
const text = `Response on your Property Listing
Dear Rahul Bond,
A user is interested in your Property, ID 62455995: 2 BHK , Multistorey
Apartment in Raheja Vihar , Mumbai.
Details of Contact Made:
Sender's Name: Test (Individual)`;
const rgx = /(?<=A user is interested in your Property, ID \d : )(.*\n.*)/g;
const result = text.match(rgx);
console.log(result);
It works here https://regexr.com/6tulh
CodePudding user response:
From the above comment ...
"How about more generically looking for ...
ID
, followed by a whitespace (sequence), followed by a sequence of 8 digits, followed by a single colon, followed by another whitespace (sequence) and then capturing anything which is not a dot/period including the first occurring dot/period .../ID\s \d{8}\:\s (?<description>[^.] \.)/g
"
... and making use of the proposed regex ...
const multilineText =
`Response on your Property Listing
Dear Rahul Bond,
A user is interested in your Property, ID 62455995: 2 BHK , Multistorey
Apartment in Raheja Vihar , Mumbai.
Details of Contact Made:
Sender's Name: Test (Individual)`;
// see ... [https://regex101.com/r/uDFHaV/1]
const regX = /ID\s \d{8}\:\s (?<description>[^.] \.)/g;
// either ...
console.log(
[...multilineText.matchAll(regX)]
.map(({ groups: { description } }) => description)
);
// or ...
console.log(
[...multilineText.matchAll(regX)]
[0]?.groups.description
);
// ... or ...
console.log(
regX
.exec(multilineText)
?.groups.description
);
.as-console-wrapper { min-height: 100%!important; top: 0; }