最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby - Regex to match certain numbers from and to a range - Stack Overflow

programmeradmin0浏览0评论

I have tried chatgpt for a correct answer but it doesn't give me a working solution.

I want to match (only) the digits before the "m2" and only if it is preceded by the wording "landgang" (as is in this example):

"kolko 12/43 4:34, tetro gamm m2 landgang 2/2 metros tetro dento 12 343m2 psi 23"

Chatgpt gives me this solution but won't work... (?<=landgang\s)(\d+)(?=\s*m2)

Hope somebody can help me, many thanks

I have tried chatgpt for a correct answer but it doesn't give me a working solution.

I want to match (only) the digits before the "m2" and only if it is preceded by the wording "landgang" (as is in this example):

"kolko 12/43 4:34, tetro gamm m2 landgang 2/2 metros tetro dento 12 343m2 psi 23"

Chatgpt gives me this solution but won't work... (?<=landgang\s)(\d+)(?=\s*m2)

Hope somebody can help me, many thanks

Share Improve this question edited Feb 6 at 21:41 engineersmnky 29.4k2 gold badges41 silver badges62 bronze badges asked Feb 6 at 10:38 FokkerFokker 113 bronze badges 6
  • Your digits in bold are not followed by the word "landgang". Anyway, please read regex' tag wiki which contains a lot of regex resources as well as instructions on how to properly ask regex questions on this site. – Friedrich Commented Feb 6 at 10:41
  • Yes, "landgang", the wording, is in the phrase followed by the "m2" – Fokker Commented Feb 6 at 10:43
  • Please edit to include additional information in your question, don't do so in comments. Also tag the programming language or regex engine you're using. – Friedrich Commented Feb 6 at 10:55
  • 3 Fairly simple with a capture group landgang.*?(\d[\d ]+)m2 regex101.com/r/VNdRr2/1 – CAustin Commented Feb 6 at 16:20
  • Many thanks CAustin. Working! – Fokker Commented Feb 7 at 11:49
 |  Show 1 more comment

2 Answers 2

Reset to default 1

If you don't mind the spaces being matched (as the one after 12) this will do

(?<=landgang.*)[ \d]+(?=m2)

Note that this is using a non fixed width pattern in the lookaround which supported by some (js,c#) but not all regex engines. https://regex101.com/r/JJppI1/1

I have assumed that the object is to match a string of digits and white spaces that must:

  • begin with a digit;
  • must not be immediately preceded by a digit or by one or more white spaces that are immediately preceded by a digit;
  • must be preceded earlier in the string by the string "landgang" that is preceded and followed by word boundaries; and
  • must be immediately followed by the string "m2" that is followed by a word boundary.

You can match the desired string, provided the conditions above are satisfied, with the following regular expression.

\blandgang\b.*?\K\d[\d\s]*(?=m2\b)

See Rubular and Regex101.

Note that this expression does not employ a capture group. Naturally, if the assumptions I have made are incorrect the expression would have to be changed accordingly.

This approach is similar to that suggested by @CAustin in a comment on the question. It uses a capture group. Capturing is often faster than matching, but it is less pleasing to some coders (a known type of personality disorder).

The regular expression can be broken down as follows.

\b         # match a word boundary
landgang   # match s string
\b         # match a word boundary
.*?        # match zero or more digits, as few as possible
\K         # reset the start of the match and discard all 
           # consumed (previously matched) tokens
\d         # match a digit
[\d\s]*    # match zero or more digits or white spaces 
(?=m2\b)   # a *positive lookahead* asserts that the current match
           # is folled by "m2" followed by a word boundary
发布评论

评论列表(0)

  1. 暂无评论