6
\$\begingroup\$

I have a string like this :

val content = "some_text{macro1}another_text{macro2}text"

I want to replace the {macro1} and {macro2} with macro1 and macro2, i.e. just to remove the { and }.

I wrote some piece of code which works fine, but for me it seems very hard to read:

val Pattern = """\{(.*)\}""".r
Pattern.findAllIn(content).matchData.foldLeft(content) ( (newContent: String, current: Regex.Match) => {
 newContent.replace(current.group(0), current.group(1))
 }
)

How I can improve this code? Please note: since it's in Scala, I prefer it in the functional way.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 26, 2015 at 21:11
\$\endgroup\$
2
  • \$\begingroup\$ Related Stack Overflow question \$\endgroup\$ Commented May 26, 2015 at 21:36
  • \$\begingroup\$ Do you want to remove only matched curly braces? \$\endgroup\$ Commented May 26, 2015 at 23:17

4 Answers 4

7
\$\begingroup\$

Your desire to use the "functional way" is not well-motivated. Why do it "the functional way" when the "other way" is not only easier to read, but also common practice, and well-understood?

val stripCurly = "[{}]".r
val replaced = stripCurly.replaceAllIn(a, "")

If you want to have forced-matching of the braces consider:

val pure = """\{([^}]*)\}""".r
val pured = pure.replaceAllIn(content, "1ドル")

Note the use of the "not a } inside the {}" logic in the regex.

The examples above are running here in ideone

answered May 27, 2015 at 2:18
\$\endgroup\$
6
\$\begingroup\$

I know nothing of Scala, and my Regex knowledge is that of the .NET flavor, so please take this with a grain of salt.

In my books, your regex produces only 1 match:

  • {macro1}another_text{macro2}

You're matching an opening brace and a closing brace, but in-between, anything goes, including opening and closing braces!

To get 2 matches you could use a lazy/reluctant match for the .* part:

\{(.*?)\}

This produces 2 matches:

  • {macro1}
  • {macro2}

However an excluding match is a better option because it doesn't incur backtracking and thus, performs better.

answered May 27, 2015 at 2:10
\$\endgroup\$
3
\$\begingroup\$

I know this is late, but here's a Scala flavoured way:

Define a function that takes a string as a parameter, filters out all the characters you don't want (in this case { and }), and then returns a new string: val bracketRemover = (str:String) => str.filter(_!='{').filter(_!='}')

val targetStr = "This { should }{ not contain { } {brack{ets"

and then just call the function with your target string:

scala> val cleanString = bracketRemover(targetStr)
cleanString: String = This should not contain brackets
answered Aug 7, 2017 at 11:21
\$\endgroup\$
0
\$\begingroup\$

Regarding your question, it seems that you should parse the string and remove {, } from your given string. This simple algorithm just removes the two curly braces from the string.

String a="some_text{macro1}another_text{macro2}text";
StringBuilder newString;
for(i=0; i<=a.size; i++){
if(!a.charAtIndex[i].equals({) OR !a.charAtIndex[i].equals(})){
 newString.apend(a.charAtIndex[i]);
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered May 26, 2015 at 22:03
\$\endgroup\$
1
  • \$\begingroup\$ it's OK, if you feel that i was wrong. \$\endgroup\$ Commented May 26, 2015 at 23:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.