Regex expression to match forward slash followed by string

给你一囗甜甜゛ 提交于 2019-12-11 16:54:59

问题


I need to filter a string, if it contains three strings:

/A/ABC
A_ABC
Downloaded

I have two types of lines. All contain Downloaded, but only either /A/ABC or A_ABC

Every substring has spaces to their left and right.

What are the correct regex expressions to match a line, if it contains Downloaded and ABC?

My big problem to match for ABC are either the slash or underscore on its left side.

I tried the following expression:

'\bDownloaded\b + \b/A/ABC\b | \/ABC\b'

However I don't receive the matching lines. Maybe someone has an easy fix to my try. Thank you!

/A/ and A_ are only examples. There could be any other letter or multiple letters. I just need to know if there is ABC anywhere in the line.

To be clear: I checked several other posts, which were close to my problem, but couldn't finally solve mine.


回答1:


You could use a positive lookahead and assert the word Downloaded, then match ABC in the string

^(?=.*\bDownloaded\b).*ABC.*$

Regex demo

That will match:

  • ^ Assert start of the string
  • (?=.*\bDownloaded\b) Positive lookahead, assert what follows is the word Downloaded between word boundaries
  • .*ABC.* Match any character 0+ times, then ABC followed by any character 0+ times
  • $ Assert end of string


来源:https://stackoverflow.com/questions/55520865/regex-expression-to-match-forward-slash-followed-by-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!