在字符串问题结束时匹配。 [英] Matching at end of string question.

查看:169
本文介绍了在字符串问题结束时匹配。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的文字如下所示:



John Paul F,


Smith John



我需要得到起始单词和尾随非单词,如果存在于单独的变量中。 我试过



(。*)(\ W * $)



但它返回整个字符串在包含逗号的第一个变量中。 如何编写此表达式。



谢谢

解决方案

< font face = Arial size = 2>


以下内容将提取字符串末尾可能出现的第一个单词和任意两个字符。没有非单词正则表达式,有一个非单词字符表达式\W将匹配[^ a-zA-Z0-9_]你必须假设一个非单词由两个字符串组成或者三个字符(适用于大多数情况,即(第二个),(F,))或者您可以假设最后一个字符是非单词字符,可能也不起作用。以下内容将匹配字符串末尾的第一个单词和非单词(通过双字符方法):



(^ \w *)|(\s [\w\W] {2}


 

I have text in a column like the following:

 

John Paul F,

Smith John

 

I need to get the beginning words and the trailing non word if present into separate variables.  I have tried

 

(.*)(\W*$)

 

But it the entire string is returned in the first variable including the comma.  How should this expression be written.

 

Thank you

解决方案

 

The following will extract the first word and any two characters that might be on their own at the end of the string. There is no non-word regex expressions, there is a non-word character expression \W which will match [^a-zA-Z0-9_] You have to make an assumption that a non word consists of either a string of two or three characters (will work for most cases, i.e. (2nd),(F, ) ) or you can assume that the last character is a non-word character which might not work as well. The following will match the first word and the non-word (via the two character method) at the end of the string:

 

(^\w*)|(\s[\w\W]{2}


)


这篇关于在字符串问题结束时匹配。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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