开发者

Regex stop group after word

I have a string that looks something like this:

2[1:22:33] Downloaded file: sometextand41245 Original File: 1234_MyFile.ext spaces...

and a regex that almost works to get the group after Original File:space and spaces...

.+Original Name:\s*(.+)\s*

this will give me 2 groups, one has开发者_JS百科 everything and the other gives me the group I want plus more:

1234_MyFile.ext space space space...

I am using the word space to represent the character... I appologize if this doesnt make much sence as it is late. Here is the result I want:

1234_MyFile.ext

thank you


You can use \S to match any non-whitespace characters:

.+Original Name:\s*(\S+)\s*

However that won't really do what you want, if the original name can contain spaces in the end, so there might be better alternatives. In your example there are spaces after the original name, is there something after those spaces? Or does the string end after the spaces? If it ends after the spaces, you can do something like:

Original Name:\s*(.+?)\s*$

If it does not end after the spaces, substitute the $ with what comes after them.


Try this:

.+Original\sFile:\s(.*)\s*

Tested and works at http://www.regextester.com/


You could be more specific:

Original Name:\s*(\S+)

\S+ matches one or more non-space characters.

Or you could make your quantifier lazy, but then you need to enforce that at least one space or the end of the string match after the filename:

Original Name:\s*(.+?)(?:\s+|$)


In regex world there can be lots of working pattern variants (and that's what I love!) but my advise is to be as specific as possible. I try to avoid inventing crazy regexes that cover any possible situation if I know source text well enough ;) My 5 cents (with positive lookbehind):

(?<=Original Name:\s*)(\S+)

For my regex I assume that file name can be everything but space characters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜