0

I have a string of text like the following:

Image 56 ::: Beach, Summer, Sun !!! John Smith ???

I'd like to extract just John Smith

Ive tried (?s)(?<=\!\!\!).+?(?=\?\?\?})

and (\!\!\!)(?:[^])*?(\?\?\?) but the first one doesn't work, and the second one gives the result !!! John Smith ??? not just John Smith. Can you offer some help? Thank you

Nova
  • 403
  • 1
  • 8
  • 32
  • 1
    Not _every_ job requires a hammer :-) Have you thought of perhaps simply using string functions to locate and extract the data? It's likely to be far more readable. – paxdiablo Jan 13 '15 at 01:05
  • No idea what a hammer is, nor what you mean :) Sorry I'm just starting to lerrn regex tonight. Will look into those terms though, cheers – Nova Jan 13 '15 at 02:53
  • a hammer is a tool used for banging in nails, it's not some new sort of IT thing :-) The saying I'm referring to is "if the only tool you have is a hammer, every problem looks like a nail". And what I mean is that, maybe, regex is not the right tool for this job. – paxdiablo Jan 13 '15 at 03:09

4 Answers4

2

You can use a capturing group with a negated character class.

!!!\s*([^?]+?)\s*\?{3}

To display the match result you need to reference the first group index.

Live Demo

Community
  • 1
  • 1
hwnd
  • 65,661
  • 4
  • 77
  • 114
1

There's a stray } character in your first pattern. Remove that and it'll work.

Aran-Fey
  • 30,995
  • 8
  • 80
  • 121
1

Try this :

!!!\s+\K.*?(?=\s+\?{3})

Check https://regex101.com/r/uF0gV8/1 and Support of \K in regex

EXPLANATIONS:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  !!!                      '!!!'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  \K                       restart the match
----------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    \?{3}                    '?' (3 times)
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
Community
  • 1
  • 1
Gilles Quenot
  • 143,367
  • 32
  • 199
  • 195
0

Try this:

!!!\s*\K[^\n\r?]+?(?=\s*\?\?\?)
Andie2302
  • 4,551
  • 3
  • 19
  • 39