3

I found the following code from this question, regex to match everything until it finds 2 upper case characters?

^.*(?=\b(?:[^\sA-Z]*[A-Z]){2})

however my question is slightly different then the OP

I want to match everything up to the upper case in the following string, the rules should match everything until it negative lookaround finds 2 uppercase characters and then match everything inbetween from the 1st uppercase until the start of the 2nd uppercase character

so I Want (continue from op example)

Http is an HttpHeader

is to get Http is an Http

instead of Http is an which OP is getting in posted thread

Community
  • 1
  • 1
user1973125
  • 83
  • 1
  • 3
  • 2
    Can you clarify your rules? Your example doesn't match your description as matching everything until uppercase would return nothing as the first uppercase is the first character – psubsee2003 Dec 22 '13 at 01:31
  • Agree with @psubsee2003 -- the result you want contains *two* upper cased characters! – Explosion Pills Dec 22 '13 at 01:34
  • possible duplicate of [RegEx to split camelCase or TitleCase (advanced)](http://stackoverflow.com/questions/7593969/regex-to-split-camelcase-or-titlecase-advanced) – mvp Dec 22 '13 at 02:14
  • @mvp does not actually work as expected see http://regex101.com/r/bO7aP8 – user1973125 Dec 22 '13 at 02:53
  • Use `(?<! )(?=[A-Z])` – hwnd Dec 22 '13 at 05:50

4 Answers4

0

Seems overly comp. to me

preg_match( '/[^A-Z]+/', $str, $res );
Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48
0
preg_match('/[^A-Z]*([A-Z]{1}[^A-Z]*[A-Z]{1}[^A-Z]*)/', $str, $res);
Jompper
  • 1,412
  • 9
  • 15
0

use this pattern ^.*?(?=\b(?:[^\sA-Z]*[A-Z]){2}).+?(?=[A-Z]) Demo

alpha bravo
  • 7,838
  • 1
  • 19
  • 23
0
([A-Z].*?\w+(?=[A-Z]))

You may follow the above regex. That's so simple and yet fast. See matched groups here: Live demo

revo
  • 47,783
  • 14
  • 74
  • 117