1

I need to replace string 'name' with fullName in the following kind of strings:

software : (publisher:abc and name:oracle)

This needs to be replaced as:

software : (publisher:abc and fullName:xyz)

Now, basically, part "name:xyz" can come anywhere inside parenthesis. e.g.

software:(name:xyz)

I am trying to use groups and the regex I built looks :

(\bsoftware\s*?:\s*?\()((.*?)(\s*?(and|or)\s*?))(\bname:.*?\)\s|:.*?\)$)
2
  • 1
    Please check regex101.com/r/81r4Wq/2, \b(software\s*:\s*\([^()]*)\bname:\w+ => $1fullName:xyz Commented Jul 12, 2019 at 10:37
  • Thanks, that works. Can you post it as an answer? Commented Jul 12, 2019 at 11:35

1 Answer 1

1

You may use

\b(software\s*:\s*\([^()]*)\bname:\w+

and replace with $1fullName:xyz. See the regex demo and the regex graph:

enter image description here

Details

  • \b - word boundary
  • (software\s*:\s*\([^()]*) - Capturing group 1 ($1 in the replacement pattern is a placeholder for the value captured in this group):
    • software - a word
    • \s*:\s* - a : enclosed with 0+ whitespaces
    • \( - a ( char
    • [^()]* - 0 or more chars other than ( and )
  • \bname - whole word name
  • : - colon
  • \w+ - 1 or more letters, digits or underscores.

Java sample code:

String result = s.replaceAll("\\b(software\\s*:\\s*\\([^()]*)\\bname:\\w+", "$1fullName:xyz");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.