正则表达式匹配字符串

时间:2015-09-17 10:15:10

标签: ruby regex regex-negation

我需要从以下文字中提取MyActivity

These are the steps that the user did before sending the bug, all the user touches and interactions are recorded here. \n\n 2014-07-15 13:46:02.323+0200 UTC      com.bug.demo.demoapplication.MyActivity was started \n 2014-07-15 13:46:27.026+0200 UTC      com.bug.demo.demoapplication.LoginActivity was started \n 2014-07-15 13:46:35.108+0200 UTC      In activity com.bug.demo.ss.ss.MyActivity.ss: View(email_field) of type android.widget.EditText received a click event \n 2014-07-15 13:46:36.692+0200 UTC      In activity com.bug.demo.demoapplication.MyActivity: View(password_field) of type android.widget.EditText received a click event \n 2014-07-15 13:47:02.922+0200 UTC      In activity com.bug.demo.demoapplication.MyActivity: View(login_begin) of type android.widget.Button received a click event \n 2014-07-15 13:47:25.013+0200 UTC    

我需要com.bug的任何MyActivity的最后一部分从该字符串与正则表达式匹配。

这是我到目前为止所尝试的内容:

(\.)\S*[^\W]

哪个匹配整个com.bug.demo.demoapplication...

如何将其细化为仅匹配MyActivity之前的点数。

3 个答案:

答案 0 :(得分:1)

\bcom\.bug(?:\.\S+)?\.(\w+)

您可以使用此功能并抓住group 1。请参阅演示。

https://regex101.com/r/vV1wW6/18

答案 1 :(得分:0)

这适用于该字符串:/([a-z]+\.)+(\w+)/m

您需要的字符串将在第2组中。

答案 2 :(得分:0)

假设您已经拥有“ com.bug.demo.demoapplication.MyActivity”的权限,则可以使用以下内容:/\.([a-z]|[A-A]|[0-9])*$/。如果希望在字符串中包含特殊字符,则可以使其更加健壮。

这将导致“ .MyActivity”。然后,您可以使用以下命令来消除前导点字符:What is the easiest way to remove the first character from a string?