0

I have a file with following information

Hi my {4 name is {4: Jeremy I {4: work in {4 Texas My job is {3 very {0 hard, I work until 4

I need to match all {4 values in this text, so I tried to write following regex: [\{4^:]

But when I try this on a regex tester, it matches all occurances of {, 4 and : but I only want the combination of {4 without a : in it.

What am I doing wrong?

4
  • 4
    All 4 occurrences? If you need to match {4 not followed with :, use String res = s.replaceAll("\\{4(?!:)", "newValue");. If you need to replace all {4, just use .replace("{4", "NEW_VALUE") - no need for a regex. Commented May 24, 2017 at 6:27
  • 1
    what do you mean ...to match all {4, you dont need regex for that... Commented May 24, 2017 at 6:27
  • 2
    Don't use [...] (character class) Commented May 24, 2017 at 6:28
  • You need to .replace("{4:", "<NEWVAL>:"). Commented May 24, 2017 at 6:46

1 Answer 1

1

Use negative lookahead to remove {4 which are followed by :. Also remove square brackets as they will match all of the results individually.

{4(?!:)

DEMO

  1. {4 matches the characters {4 literally
  2. (?!:) is negative lookahead which means look for {4 which are not followed by :
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.