regex - matching between a literal string and a quotation mark - java

I'm terrible at Regex and would greatly appreciate any help with this issue, which I think will be newb stuff for anyone familiar.
I'm getting a response like this from a REST call
{"responseData":{"translatedText":"Ciao mondo"},"responseDetails":"","responseStatus":200,"matches":[{"id":"424913311","segment":"Hello World","translation":"Ciao mondo","quality":"74","reference":"","usage-count":50,"subject":"All","created-by":"","last-updated-by":null,"create-date":"2011-12-29 19:14:22","last-update-date":"2011-12-29 19:14:22","match":1},{"id":"0","segment":"Hello World","translation":"Ciao a tutti","quality":"70","reference":"Machine Translation provided by Google, Microsoft, Worldlingo or the MyMemory customized engine.","usage-count":1,"subject":"All","created-by":"MT!","last-updated-by":null,"create-date":"2012-05-14","last-update-date":"2012-05-14","match":0.85}]}
All I need is the 'Ciao mondo' in between those quotations. I was hoping with Java's Split feature I could do this but unfortunately it doesn't allow two separate delimiters as then I could have specified the text before the translation.
To simplify, what I'm stuck with is the regex to gather whatever is inbetween translatedText":" and the next "
I'd be very grateful for any help

You can use \"translatedText\":\"([^\"]*)\" expression to capture the match.
The expression meaning is as follows: find quoted translatedText followed by a colon and an opening quote. Then match every character before the following quote, and capture the result in a capturing group.
String s = " {\"responseData\":{\"translatedText\":\"Ciao mondo\"},\"responseDetails\":\"\",\"responseStatus\":200,\"matches\":[{\"id\":\"424913311\",\"segment\":\"Hello World\",\"translation\":\"Ciao mondo\",\"quality\":\"74\",\"reference\":\"\",\"usage-count\":50,\"subject\":\"All\",\"created-by\":\"\",\"last-updated-by\":null,\"create-date\":\"2011-12-29 19:14:22\",\"last-update-date\":\"2011-12-29 19:14:22\",\"match\":1},{\"id\":\"0\",\"segment\":\"Hello World\",\"translation\":\"Ciao a tutti\",\"quality\":\"70\",\"reference\":\"Machine Translation provided by Google, Microsoft, Worldlingo or the MyMemory customized engine.\",\"usage-count\":1,\"subject\":\"All\",\"created-by\":\"MT!\",\"last-updated-by\":null,\"create-date\":\"2012-05-14\",\"last-update-date\":\"2012-05-14\",\"match\":0.85}]}";
System.out.println(s);
Pattern p = Pattern.compile("\"translatedText\":\"([^\"]*)\"");
Matcher m = p.matcher(s);
if (!m.find()) return;
System.out.println(m.group(1));
This fragment prints Ciao mondo.

use look-ahead and look-behind to gather strings inside quotations:
(?<=[,.{}:]\").*?(?=\")
class Test
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String in = scanner.nextLine();
Matcher matcher = Pattern.compile("(?<=[,.{}:]\\\").*?(?=\\\")").matcher(in);
while(matcher.find())
System.out.println(matcher.group());
}
}

Try this regular expression -
^.*translatedText":"([^"]*)"},"responseDetails".*$
The matching group will contain the text Ciao mondo.
This assumes that translatedText and responseDetails will always occur in the positions specified in your sample.

Related

Regex look ahead to seperate string into tokens

I currently have the following code which allows me to find matches from a String.
I need to be able to find all words similar to 64xand split them up into tokens, so I'll get 64 and x as the output.
I have looked at regexs lookahead and this does not solve the issue, is there a way to do this without creating a new arraylist to store matches similar to 64x then splitting them up?
String input = "Hello world 65x";
ArrayList<String> userInput = new ArrayList<>();
Matcher isMatch = Pattern.compile("[0-9]*+[a-zA-Z]")
.matcher(input);
while (isMatch.find()) {
userInput.add(isMatch.group());
}
You can try the following regular expression:
\b(\p{Digit}+)(\p{Alpha})\b
Additionally, if you plan to use the regular expression very often, it is recommended to use a constant in order to avoid recompile it each time, e.g.:
private static final Pattern REGEX_PATTERN =
Pattern.compile("\\b(\\p{Digit}+)(\\p{Alpha})\\b");
public static void main(String[] args) {
String input = "Hello world 65x";
Matcher matcher = REGEX_PATTERN.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
}
Output:
65
x
No need of lookaheads, you can use nested captured groups:
Matcher isMatch = Pattern.compile("\\b([0-9]+)([a-zA-Z])\\b");
Group #1 will contain 65 and group #2 will contain x.
Better to add \\b (word boundary) on either side to avoid matching abc56xyz
You just need to use Matcher.group(int). This lets you extract pieces of the matched text. Read about caputring groups here. A regex that contains capturing groups is \\b([0-9]+)([a-zA-Z])\\b (as given by anubhava).

How to crop multiple text from String in Java?

I want to crop a portion of String:
" this is Test [ABC:123456] Sting with multiple properties [ABC:98765] ..."
So in result i want to crop String between "[ ]". {Here ABC:12345 and ABC:98765}
Note There can be n number of property.
what is the Best way to get result.
public static void main(String[] args) {
String input = "test bla [ABC56465:asd] asdasdqwd [DEF:345]";
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
Matcher match = pattern.matcher(input);
while(match.find()){
System.out.println(match.group());
}
}
Follow the Tutorials from Niels. This could be solution.
To get the output without the "[ ]" just replace:
System.out.println(match.group());
With:
System.out.println(match.group(1));
as mentioned in the comments.
You need to define you pattern and do a regex matching, extrating the group you need.
See http://tutorials.jenkov.com/java-regex/matcher.html
for a tutorial on regex. (Especially the find() start() and end() section)
In your case the pattern should be very simple.
In Java, you can do this:
String resultString = subjectString.replaceAll("\\[[^\\]]*\\]", "");
Explanation
\[ matches the opening bracket
The negated character class [^\]]* matches any character that is not a closing bracket
\] matches the closing bracket
We replace with the empty string

Regular expression -- ignore part of a matched string

I have text that I want to fined some thing like this
"Name DAVID"
I want to match "DAVID" in this, larger, text.
I tried use a regular expression like this:
(Name(.*))
and also
(?:Name(.*))
but this also matched "Name," and I only want to match "David".
Just drop the extra parens:
"Name (.*)"
Even that is probably excessive, you probably want something more like:
"Name (\w*)"
to catch exactly the characters that you want.
You need to use a Matcher. This snippet of code worked for me
public static void main(String[] asdf) {
String text = "NAME David";
Pattern p = Pattern.compile("NAME (.+)");
Matcher m = p.matcher(text);
if (m.matches()){
System.out.println(m.group(1));
}
}
Note that m.matches() is mandatory or m.group(1) will throw java.lang.IllegalStateException: No match found
String's matches()
public static void main(String[] args) {
String regex = "^Name(.+)$";
System.out.println("Name".matches(regex));
System.out.println("Name MIKE".matches(regex));
System.out.println("Name DAVID".matches(regex));
}
For some reason, it doesn't look like your question had yet been answered with a correct regex for what you requested.
Here is what you are looking for:
(?<=Name )DAVID
This only matches DAVID, in the proper context (see demo).
You probably know this, but this is a way to test a string with this regex:
Pattern regex = Pattern.compile("(?<=Name )DAVID");
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.find();
Explain Regex
(?<= # look behind to see if there is:
Name # 'Name '
) # end of look-behind
DAVID # 'DAVID'

java regular expression

Can anyone please help me do the following in a java regular expression?
I need to read 3 characters from the 5th position from a given String ignoring whatever is found before and after.
Example : testXXXtest
Expected result : XXX
You don't need regex at all.
Just use substring: yourString.substring(4,7)
Since you do need to use regex, you can do it like this:
Pattern pattern = Pattern.compile(".{4}(.{3}).*");
Matcher matcher = pattern.matcher("testXXXtest");
matcher.matches();
String whatYouNeed = matcher.group(1);
What does it mean, step by step:
.{4} - any four characters
( - start capturing group, i.e. what you need
.{3} - any three characters
) - end capturing group, you got it now
.* followed by 0 or more arbitrary characters.
matcher.group(1) - get the 1st (only) capturing group.
You should be able to use the substring() method to accomplish this:
string example = "testXXXtest";
string result = example.substring(4,7);
This might help: Groups and capturing in java.util.regex.Pattern.
Here is an example:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Example {
public static void main(String[] args) {
String text = "This is a testWithSomeDataInBetweentest.";
Pattern p = Pattern.compile("test([A-Za-z0-9]*)test");
Matcher m = p.matcher(text);
if (m.find()) {
System.out.println("Matched: " + m.group(1));
} else {
System.out.println("No match.");
}
}
}
This prints:
Matched: WithSomeDataInBetween
If you don't want to match the entire pattern rather to the input string (rather than to seek a substring that would match), you can use matches() instead of find(). You can continue searching for more matching substrings with subsequent calls with find().
Also, your question did not specify what are admissible characters and length of the string between two "test" strings. I assumed any length is OK including zero and that we seek a substring composed of small and capital letters as well as digits.
You can use substring for this, you don't need a regex.
yourString.substring(4,7);
I'm sure you could use a regex too, but why if you don't need it. Of course you should protect this code against null and strings that are too short.
Use the String.replaceAll() Class Method
If you don't need to be performance optimized, you can try the String.replaceAll() class method for a cleaner option:
String sDataLine = "testXXXtest";
String sWhatYouNeed = sDataLine.replaceAll( ".{4}(.{3}).*", "$1" );
References
https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#using-regular-expressions-with-string-methods

java regular expression get substring

I can't find any good resource for parsing with regular expression. Could someone please show me the way.
How can I parse this statement?
"Breakpoint 10, main () at file.c:10"
I want get the substring "main ()" or 3rd word of the statement.
This works:
public void test1() {
String text = "Breakpoint 10, main () at file.c:10";
String regex = ",(.*) at";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
Basically the regular expression .(.*) at with group(1) returns the value main ().
Assuming you want the 3rd word of your string (as said in your comments), first break it using a StringTokenizer. That will allow you to specify separator (space is by default)
List<String> words = new ArrayList<String>();
String str = "Breakpoint 10, main () at file.c:10";
StringTokenizer st = new StringTokenizer(str); // space by default
while(st.hasMoreElements()){
words.add(st.nextToken());
}
String result = words.get(2);
That returns main
If you also want the (), as you defined spaces as separator, you also need to take the next word words.get(3)
Good website regular-expressions.info
Good online tester regexpal.com
Java http://download.oracle.com/javase/tutorial/essential/regex/
I turn to these when I want to play with Regex
Have you seen the standard Sun tutorial on regular expressions ? In particular the section on matching groups would be of use.
Try: .*Breakpoint \d+, (.*) at
Well, the regular expression main \(\) does parse this. However, I suspect that you would like everything after the first comman and before the last "at": ,(.*) at gives you that in group(1) that is opened by the parenthesis in the expression.

Categories