提取字符串,然后使用REGEX进行空间的数字

发布于 2025-02-09 16:06:14 字数 388 浏览 0 评论 0原文

follwing字符串

[1] weight | width | depth | 5.0 kg | 6.0 mm^3 | 10.12 cm^2

我只需要从中提取单元字符串的

unit=kg
unit=mm^3
unit=cm^2

,但我也尝试了以下正则弦

(?<unit>[^ -+0-9\\.\|\[\}\]]+)

,但它也给出了重量,宽度,深度值。也尝试过

(?<unit>[\D][^|]+)

但不起作用。 我想我需要提取随后是数字和空间的字符串,

您可以帮助我

I have the follwing string

[1] weight | width | depth | 5.0 kg | 6.0 mm^3 | 10.12 cm^2

From that I need to extract the unit strings only

unit=kg
unit=mm^3
unit=cm^2

I tried the below regex

(?<unit>[^ -+0-9\\.\|\[\}\]]+)

But it is also giving the weight,width,depth values too. Also tried

(?<unit>[\D][^|]+)

but not worked.
I think I need to extract the strings which are followed by number and space

Can you help me on this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2025-02-16 16:06:14

您可以使用字符类列出允许的字符:

\b[0-9]+(?:\.[0-9]+)?[^\S\r\n]+(?<unit>[a-z^0-9]+)\b

regex demo

或更具体:

\b[0-9]+(?:\.[0-9]+)?[^\S\r\n]+(?<unit>kg|[mc]m)\b

请参阅另一个 regex demo

如果应该有管道或字符串的末端:

\b[0-9]+(?:\.[0-9]+)?[^\S\r\n]+(?<unit>[^0-9\s]\S*)(?:[^\S\r\n]+\||$)

Regex Demo

You could use a character class to list the allowed characters:

\b[0-9]+(?:\.[0-9]+)?[^\S\r\n]+(?<unit>[a-z^0-9]+)\b

Regex demo

Or more specific:

\b[0-9]+(?:\.[0-9]+)?[^\S\r\n]+(?<unit>kg|[mc]m)\b

See another regex demo

If there should be either a pipe or the end of the string:

\b[0-9]+(?:\.[0-9]+)?[^\S\r\n]+(?<unit>[^0-9\s]\S*)(?:[^\S\r\n]+\||$)

Regex demo

可爱咩 2025-02-16 16:06:14

我使用了以下以下等级,它仅匹配单元字符串。

 \b[a-z]{2}(?:[\^]\d+)?(?!\S)\b

演示链接: https://regex101.com/r/ajonqr/ajonqr/1

I have used below regex, and it is matching only unit string.

 \b[a-z]{2}(?:[\^]\d+)?(?!\S)\b

Demo link : https://regex101.com/r/AjONqR/1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文