0

accusantique et o (MLS® R2608327). Sed ut perspiciatis voluptatem mls listing :MLS&reg#:12243235435 beatae vitae dicta MLS Number#:12243235435 sunt 3543654654675645654654 explicabo .

Given the string above how could I extract "(MLS® R2608327)"or"MLS&reg#:12243235435"or"MLS Number#:12243235435" in the string? value of it might change as this is dynamic. So, other instance might be "135435". The location/sequence might also change, it could be in the middle or last part.

This regex

^ ((MLS|mls|Mls|MLS&reg|mls&reg|Mls&reg|MLS®|MLS® Number|mls Number|Mls Number|mls®|Mls®|Property ID|)[^A-Za-z0-9])?(([^?*/\:;<> ]{1,23}\d{2,23}[^?*/\:;<> ]{1,23}) *)$

This regex is working well but it is not extracting mls:#w3432423 from a string like dfnjkgfkhgb mls:#234234245 fgjhfgf 3498234789 dshfdsfgbjhsf.

Test ID
  • 11
  • 1
  • 1
    See [Reference - What does this regex mean?](/q/22937618/4642212) and the [regex tag wiki](/tags/regex/info) and use regex debuggers like [RegEx101](//regex101.com/). – Sebastian Simon Aug 27 '21 at 04:58
  • If you omit the anchors, the pattern seems to match `mls:#w3432423` from `dfnjkgfkhgb mls:#234234245 fgjhfgf 3498234789 dshfdsfgbjhsf.` Perhaps you could shorten the pattern a bit and use a case insensitive flag `/i` see https://regex101.com/r/VAvfcg/1 Note that in the example string, you get multiple matches with group 1 and group 2 which you could get for example using [matchAll](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll) – The fourth bird Aug 27 '21 at 08:58
  • Thankyou so much @Thefourthbird it works perfectly. but it is also picking up numbers, like it is picking up "3498234789" also. is there any way to put a condition in this regex that it only matches the string that starts with these three characters "MLS" – Test ID Aug 30 '21 at 05:29
  • @TestID Do you mean like this? `\b((?:MLS(?:&reg|®?(?: Number)?))[^A-Za-z0-9]*)([^?*\/\\:;<> ]{1,23}\d{2,23}[^?*\/\\:;<> ]{1,23})` https://regex101.com/r/c2Y8OV/1 – The fourth bird Aug 30 '21 at 07:36

1 Answers1

0

Using a case insentive match, you can use:

\b(MLS(?:&reg|®?(?: Number)?)[^A-Za-z0-9]*)([^?*\/\\:;<> ]{1,23}\d{2,23}[^?*\/\\:;<> ]{1,23})

The pattern in parts matches:

  • \b A word boundary to prevent a partial match
  • ( Capture group 1
    • MLS Match literally
    • (?: Non capture group
      • &reg Match literally
      • | Or
      • ®?(?: Number)? Match optional ® and optional Number
    • ) Close non capture group
    • [^A-Za-z0-9]* Match optional chars other then the listed
  • ) Close group 1
  • ( Capture group 2
    • [^?*\/\\:;<> ]{1,23} Match 1-23 repetitions of any char except the listed
    • \d{2,23} Match 2 -23 digits
    • [^?*\/\\:;<> ]{1,23} Match 1-23 repetitions of any char except the listed
  • ) Close group 2

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70