普通的前pression,提取子字符串在一些指定的格式不工作的所有可能性 [英] Regular expression that extracts sub-strings that are in some specified format not working for all possibilities

查看:125
本文介绍了普通的前pression,提取子字符串在一些指定的格式不工作的所有可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的previous问题继续在这里:需要定期EX pression,提取子字符串在一些指定的格式。该建议有解决的办法解决我的问题,但不是全部。

:我遇到另一个字符串样品从以下列格式的子字符串必须被提取出来

  

@这里的任何字符]。[任意字符这里]。[这里的任何字符] .......等等。


例如,我有一个字符串:


  

我的位置是@ XYZ大陆]。[XYZ国家] [XYZ状态]。[XYZ市。在地图上[XYZ街。你是最欢迎随时存在。


previous的解决方案是在此字符串工作的罚款。输出:

  @ [XYZ大陆]。[XYZ国家] [XYZ状态]。[XYZ市] [XYZ街]

但是,该解决方案的子串外只为 + 检查和空格(的点击这里查看演示)。但我想要的是子串的左,右必须被忽略不管它是字母数字的,空格或其他特殊字符。例如:


  

您好@ XYZ Continent1]。[XYZ国家1] + @ XYZ Continent2]。[XYZ COUNTRY2] 1 @ XYZ Continent3]。[XYZ COUNTRY3]那里。


输出必须是:

  @ [XYZ Continent1]。[XYZ国家1]
@ [XYZ Continent2]。[XYZ COUNTRY2]
@ [XYZ Continent3]。[XYZ COUNTRY3]

从previous问题解决方法:

(?!< \\ W)

  @(| [?* \\] \\ W + \\ B \\(= [\\ S +] | $))


解决方案

您可以使用以下方法:

 ; @(小于\\ W?!)((?:?\\ [[^ \\]] + \\] \\)+)

请参阅演示

Continuing from my previous question here: Need regular expression that extracts sub-strings that are in some specified format. The solution that was suggested there solved my problem but not completely. I came across another string sample from which the the sub-strings in following format must be extracted:

@[any character here].[any character here].[any character here]....... and so on.

For example, I have a string:

My location is @[XYZ Continent].[XYZ Country].[XYZ State].[XYZ City].[XYZ Street] on the map. You are most welcome there anytime.

Previous solution is working fine on this string. Output:

@[XYZ Continent].[XYZ Country].[XYZ State].[XYZ City].[XYZ Street]

But that solution only checks for + and whitespace outside the sub-string(CLICK HERE TO SEE DEMO). But what I want is that the right and left of the sub-string must be ignored no matter if it is alphnumeric, whitespace or another special character. For example:

Hello@[XYZ Continent1].[XYZ Country1]+@[XYZ Continent2].[XYZ Country2]1@[XYZ Continent3].[XYZ Country3]there.

The output must be:

@[XYZ Continent1].[XYZ Country1]
@[XYZ Continent2].[XYZ Country2]
@[XYZ Continent3].[XYZ Country3]

Solution from previous question:

(?<!\w)@(\w+\b|\[.*?\](?=[\s+]|$))

解决方案

You can use the following:

(?<!\w)@((?:\[[^\]]+\]\.?)+)

See DEMO

这篇关于普通的前pression,提取子字符串在一些指定的格式不工作的所有可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆