-1

we got some content like this: state 1rd court house, state state 2nd court house

what we want is state xxx court house

we have try lookahead/lookbehind like this: (?=state).*?(?<=court house)

but this regex extract aab like content just like ab kind content

how can i extract only ab from a string like aab

which means start with something, but the capture content do not contains it

update

sorry for the poor description

what i want to match is state 1rd court house and state 2nd court house and not match state state 2nd court house?

which means if state state 2nd court house i'd like extract the rear part state 2nd court house ignore the first state appear in the content.

Community
  • 1
  • 1
Tim Huang
  • 1
  • 2
  • Your lookaheads and lookbehinds are on the wrong sides, for one. Right now they’re looking into the capture, but you want to look out of the capture by placing the lookbehind `(?<=` *before* it and the lookahead `(?=` *after*. – Ry- Feb 01 '18 at 03:52
  • Hello, and welcome to Stack Overflow. Please note that [tag:regex] tag says: "Since regular expressions are not fully standardized, all questions with this tag should also include a tag specifying the applicable programming language or tool.". Also, please make a specific example of what you want to match and what you don't want to match, without using metasyntactic variables like `xxx`, `aab` or `ab` (unless they are a literal part of your example). Do you want to match `state 1rd court house` and `state 2nd court house` and not match `state state 2nd court house`? – Amadan Feb 01 '18 at 04:09
  • @Amadan sorry about the poor description, update the question right now – Tim Huang Feb 01 '18 at 05:39

1 Answers1

0

If I understood correctly you simply want to capture everything before court house including "court house".

Regular Expression

state(?<whatYouWant> [0-9].*?court house)*

Test String

state 1st court house, state state 2nd court house

match 1: state 1st court house
Group whatYouWant: 1st court house

match 2: state 1st court house
Group whatYouWant: 2nd court house

I assumed that the look back will stop when you hit a digit ex: "1", "2". If my assumption is wrong, send me the data that breaks the Regex.

RAFJR
  • 362
  • 1
  • 7
  • 22
  • 1
    first, thx for ur answer and sorry for my poor english, u have get part of my question. what i really want may describe as follow: **full content**: `state 1st court house, state state 2nd court house`, **what i want**: `state 1st court house` and 'state 2nd court house', in other words, i want strings start with `state` and end with `court house`, but when it comes to content like `state state 2nd court house` i don't want match the first `state` but only the rear part `state 2nd court house`. – Tim Huang Feb 01 '18 at 05:48
  • Okay thanks for the clarification. Let me update my answer for you. – RAFJR Feb 01 '18 at 10:18