简体   繁体   中英

Matches A but not B in Java Regex?

I have a big document. Lets scale it to

location=State-City-House
location=City-House

So What I want to do is replace all those not starting with State, with some other string. Say "NY". But those starting with State must remain untouched.

So my end result would be

location=State-City-House
location=NY-City-House

1.Obviously I cant use String.replaceAll().

2.Using Pattern.matcher() is tricky since we are using two different patterns where one must be found and one must not be found.

3.Tried a dirty way of replacing "location=State" first with "bocation=State" then replacing the others and then re-replacing.

So, A neat and simple way to do it?

You can definitely use replaceAll with a negative lookahead:

String repl = input.replaceAll( "(?m)^(location=)(?!State)", "$1NY-" );
  • (?m) sets MULTILINE modifier so that we match anchors ^ and $ in each line
  • (location=) matches location= and captures the value in group #1
  • (?!State) is the negative lookahead to fail the match when State appears after the captured group #1 ie location=
  • In replacement we use $1NY- to make it location=NY- at start.

RegEx Demo

If I understand your intention correctly, you don't actually have the string "State" in your input, but varying strings that represent states. But some of your text lines are missing the state altogether and only have the name of the City and House. Is that correct? In that case, the defining characteristic between the 2 kinds of lines is the number of dashes.

^location=([^-]+)-([^-]+)$

The above regex matches only full lines with only 1 dash.

I might have misunderstood the task. It would be easier if you would post some of the actual input.

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