Home > front end >  How can I extract the original String from a formatted one?
How can I extract the original String from a formatted one?

Time:01-08

Ok so, let's say I have a variable teamName = apple and teamPattern = [MW3-%s], which gives me the full name: [MW3-apple].. If I know the fullName and teamPattern is there any way to get the original teamName back?

Here's my Java code I used. (This is not a XY problem, I have to save it this way -_-) https://pastebin.com/GevJnaby

I tried using Java sub-strings but that didn't work out since patternName can be set to anything. (I heard of something along the lines of patterns/Regex, but didn't find any useful function)

CodePudding user response:

How much is guaranteed about teamPattern?

If you can be sure that teamPattern will contain exactly one formatting pattern, which will be %s, you can:

  • locate the %s within teamPattern (starts at character #index)
  • calculate the amount of text right of the embedded teamName (subtracting)
  • calculate the length of the teamName (subtracting)
  • take fullName.substring(...)

CodePudding user response:

In general, the backwards-conversion does not yield a unique result. Take, for example, the pattern "foo-%s-%s" and the result "foo-bar-baz-bang". We could have arrived here by:

  • either replacing the first "%s" with "bar-baz" and the second "%s" with "bang"
  • or replacing the first "%s" with "bar" and the second "%s" with "baz-bang".

Ideone.com demo


For the concrete pattern "[MW3-%s]", we can create a regex extractor that extracts the original teamName:

^full name: \[MW3-(?<teamName>.*)]$

regex101.com demo

Here is an example on how to use the regular expression to extract the team name:

String teamPattern = "[MW3-%s]";
String teamName = "apple";
String fullNamePattern = "full name: "   teamPattern;
String fullName = String.format(fullNamePattern, teamName);
Pattern teamExtractorPattern = Pattern.compile("^full name: \\[MW3-(?<teamName>.*)]$");
Matcher matcher = teamExtractorPattern.matcher(fullName);
if (matcher.matches()) {
  String extractedTeamName = matcher.group("teamName");
  System.out.println(extractedTeamName);
}

Ideone.com demo


We can start making the pattern more generic by e.g. replacing full name : by .*. We could even try to construct the Pattern from teamPattern, but this requires some special attention (e.g. the "[" and "-" need to be replaced with "\\\\[" and "\\\\-").

Note that this kind of pattern will stop to function if a line has more than one ] after the initial full name: [-part (due to the greedy nature of regex's *, if we make it non-greedy, there are other corner cases to consider). This kind of issue would be preventable if we, for example, knew or could assure (not assume (urbandictionary.com)) that the teamName consists of only letters and digits. In this case, we could change the regex to:

^full name: \[MW3-(?<teamName>[a-zA-Z0-9]*)]$

regex101.com demo

If we also allow "_" in teamNames, the regular expression simplifies even further:

^full name: \[MW3-(?<teamName>\w*)]$

regex101.com demo

  •  Tags:  
  • java
  • Related