0

Suppose I have a String, called text, which contains the following :

blabla="A_VALUE"

Is it possible, with pattern matching, to directly retrieve the value inside the quotation marks?

For example : something similar to Format String, where you could write a pattern, %s, and then get that value.

Right now, a workaround I found is :

text = text.replace("blabla=","");
text = text.replaceAll("\"","");

However, this is very ugly.

Note : It doesn't have to be java, I want to know if the concept exists, and if so, what name does it have.

This post offers some insight, although I'm unsure what \\\\#\\s*(\\S+?)\\s* is suppose to mean

Maude
  • 512
  • 3
  • 8
  • 23
  • To be clear, are you trying to retrieve `A_VALUE` from the `String`? – Logan Aug 13 '18 at 18:11
  • Possible duplicate of: https://stackoverflow.com/questions/8430022/what-is-the-java-equivalent-of-sscanf-for-parsing-values-from-a-string-using-a-k – Patrick Parker Aug 13 '18 at 18:21

2 Answers2

1

In there is sscanf which is similar to what you described.

However Java has a Scanner class instead of such a function.

Another alternative is to use Regular Expressions then inspect the relevant Matcher group, as described here: what is the Java equivalent of sscanf for parsing values from a string using a known pattern?

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • Interesting link. I used pattern and regex somewhere else but would get match.start and then struggle to format the match.end to remove unwanted characters. Let's suppose my pattern here is "blabla=", then I still need to do some work afterwards to get the actual value. I was missing the 'group' keyword, which looks like it allows you to fetch the data inside the pattern. Useful link I found : https://stackoverflow.com/questions/16517689/confused-about-matcher-group-in-java-regex – Maude Aug 19 '18 at 13:40
1

Do you mean get the content of strings including any escaped double-quotes?

If so, then this pattern should work for you: "(\\.|[^"])*"

It means: find a double quote then find zero or more occurrences of any escaped character or anything that's not a double-quote until another quote is found.

Broken down:

  • " - find a double quote
  • \\. - find any escaped character
  • [^"] - find any character that's not a double quote
  • (x|y)* - find x or y zero or more times
  • (\\.|[^"])* - find any escaped char or any non-double-quote character zero or more times.

It will find:

Source                        Result
----------------------------+---------------
var str1 = "a string";      | "a string"
var str2 = "a \" string"    | "a \" string"
var str3 = "";              | ""
var str4 = "a string \\";   | "a string \\"
xtratic
  • 4,600
  • 2
  • 14
  • 32
  • 1
    won't this fail if you have an escaped backslash as the last character? – Patrick Parker Aug 13 '18 at 18:24
  • 1
    addendum to my previous comment: And there are more quoted string later in the input. – Patrick Parker Aug 13 '18 at 18:36
  • @PatrickParker Yes, good call. I've made an edit correcting this. – xtratic Aug 13 '18 at 18:42
  • This line text = text.replaceAll("\"",""); shows that I'm actually removing the quotation marks. Which is why I was stucked using pattern matching since what I need to find is simply a String. Your technic would however work, given that I remove the " afterwards in my answer! – Maude Aug 19 '18 at 13:42
  • @Maude yes, it could be easily stripped off afterwards. Or, rather than matching a quotemark, instead you could match the zero-width position after/before a quotemark, using the positive lookbehind/lookahead regexp syntax. – Patrick Parker Aug 19 '18 at 17:43