简体   繁体   中英

Unable to match regex

I have got the following phrase:

Mark: 5   Mike   2015-01-20

and I need to get Mike from it. I have tried to use negative lookups like this:

/^(\s+)((?!Mark\:(\d)).)*&(\w+)$/is

but it did not produce the desired result. Also there are some spaces in the begginning and the end of the phrase. Any ideas how to fix that would be welcome. Thank you.

Assuming that Mark:<optional spaces><digits><optional spaces> is a prefix for the search string (our " Mike "), and the search string is a sequence of non-space characters, then the following should do:

preg_match('/Mark\:\s*\d+\s*(\S+)/', $the_string, $m);
// $m[1] = 'Mike'

If the search string is supposed to be something more complex, then just replace \\S+ with the desired pattern. You haven't specified this case, so I can't help more.

If you search for the pattern ^\\s*Mark: \\d+\\s+(\\S+)\\s+\\S+\\s*$ , then the first group will match what your looking for. See the demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM