简体   繁体   English

使用正则表达式从字符串中提取 4 位数字?

[英]Using Regular Expression to extract 4 digits from a string?

Hoping for some help.希望得到一些帮助。 I am new to regex and I am trying to come up with something that will search a string and check if it has 4 consecutive digits, and if so extract that 4 digit number into a new attribute.我是 regex 的新手,我试图想出一些可以搜索字符串并检查它是否有 4 个连续数字的东西,如果有,则将该 4 位数字提取到一个新属性中。

I want to use a regular expression, but I'm a bit confused with the expression.我想使用正则表达式,但我对表达式有点困惑。 For some background, I'm using a master data management tool that has it's own syntax quite similar to SQL.对于某些背景,我正在使用一个主数据管理工具,它的语法与 SQL 非常相似。

This is the framework of the expression from the tool: REGEXP_LIKE(string, pattern, parameter)这是来自工具的表达式框架: REGEXP_LIKE(string, pattern, parameter)

Something like (string,/d/d/d/d,[,i]) ?(string,/d/d/d/d,[,i])

Pulling 4 digits from the string, not case sensitive (not sure if that would even be applicable)从字符串中提取 4 位数字,不区分大小写(不确定这是否适用)

Sometimes the location of the digits are different, so a substring isn't the best option here.有时数字的位置不同,因此子字符串不是这里的最佳选择。

Any feedback would be helpful to get me in the right direction!任何反馈都有助于让我朝着正确的方向前进!

Try using the expression (\\d{4}) that will match any occurrence of 4 digits in a row in a string.尝试使用表达式(\\d{4})来匹配字符串中连续出现的任何 4 位数字。 \\d refers to the set of 0-9 and the {4} is to specify 4 digits. \\d指的是0-9的集合, {4}是指定 4 位数字。 If you want to be 4 or less you'd say \\d{,4} .如果你想成为 4 或更少,你会说\\d{,4} I'd also recommend checking out a website like https://regexr.com/ , you can make expressions and then copy them elsewhere, and it shows a step by step on what each element means, and allows you to test it right there, and has a detailed cheat sheet about each feature.我还建议您查看https://regexr.com/ 之类的网站,您可以制作表达式,然后将它们复制到其他地方,它会逐步显示每个元素的含义,并允许您在那里进行测试,并有关于每个功能的详细备忘单。 The parentheses around the expression specify that it is a group, so you can later receive the contents of it by getting the first group, witch whatever API you are using.表达式周围的括号指定它是一个组,因此您可以稍后通过获取第一个组来接收它的内容,无论您使用的是什么 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM