How can i get Strings between double quotes using Regex in Java?
_settext(_textbox(0,_near(_span("My Name"))) ,"Brittas John");
ex: I need My Name and Brittas John
Get the matched group from index 1 that is captured by enclosing inside the parenthesis (...)
"([^"]*)"
DEMO
Pattern explanation:
" '"'
( group and capture to \1:
[^"]* any character except: '"' (0 or more times) (Greedy)
) end of \1
" '"'
sample code:
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher("_settext(_textbox(0,_near(_span(\"My Name\"))) ,\"Brittas John\");");
while (m.find()) {
System.out.println(m.group(1));
}
Try this regex..
public static void main(String[] args) {
String s = "_settext(_textbox(0,_near(_span(\"My Name\"))) ,\"Brittas John\");";
Pattern p = Pattern.compile("\"(.*?)\"");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
}
O/P :
My Name
Brittas John
Related
I would like to split the following string by commas using a DOTALL regex pattern what will accept letters, numbers, whitespaces and special characters such as underscores and asterisks i.e. #input("Test_1, Test_TWO , TEST_THIRTY_3*") so the output would look like:
"Test_1",
"Test_TWO",
"TEST_THIRTY_3*"
public static void main(String args[])
{
String line = "#input(\"Test_1,Test_TWO , TEST_THIRTY_3*\"\\)\";
String pattern = "#input(\"(.*?)\".*";
Pattern r = Pattern.compile(pattern, Pattern.DOTALL);
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println("Found word: " + m.group(1) );
}
You have to escape the ( by \( so your regex should look like this #input\(\"(.*?)\".*, second you can use \s*,\s* to split the result like this :
String line = "#input(\"Test_1,Test_TWO , TEST_THIRTY_3*\"\\)";
String pattern = "#input\\(\"(.*?)\".*";
Pattern r = Pattern.compile(pattern, Pattern.DOTALL);
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println(Arrays.toString(m.group(1).split("\\s*,\\s*")));
//----------------------------------------------------^^^^^^^^
}
outputs
[Test_1, Test_TWO, TEST_THIRTY_3*]
If you do not have to stick to regex you might just take the string methods.
List<String> output = Arrays.asList(line.split(","));
I have below string:
String line = put retur#ERns between #errf #fgrf#re paragraphs #fg^%tg2#785Ty*;
How can I get below values with regex:
#ERns
#errf
#fgrf
#re
#fg^%tg2
#785Ty*
My code:
String pattern = "^#\S+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
Log.i("log", m.group());
}
You can use this regex instead:
#[^#\s]*
RegEx Demo
Negated character class [^#\s] matches a character that is not # and not a whitespace.
In Java use:
final String pattern = "#[^#\\s]*";
I have a below string which comes from an excel column
"\"USE CODE \"\"Gef, sdf\"\" FROM 1/7/07\""
I would like to set regex pattern to retrieve the entire string,so that my result would be exactly like
"USE CODE ""Gef, sdf"" FROM 1/7/07"
Below is what I tried
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "\"USE CODE \"\"Gef, sdf\"\" FROM 1/7/07\", Delete , Hello , How are you ? , ";
String line2 = "Test asda ds asd, tesat2 . test3";
String dpattern = "(\"[^\"]*\")(?:,(\"[^\"]*\"))*,|([^,]+),";
// Create a Pattern object
Pattern d = Pattern.compile(dpattern);
Matcher md = d.matcher(line2);
Pattern r = Pattern.compile(dpattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: 0 " + m.group(0) );
// System.out.println("Found value: 1 " + m.group(1) );
//System.out.println("Found value: 2 " + m.group(2) );
} else {
System.out.println("NO MATCH");
}
}
}
and the result out of it breaks after ,(comma) and hence the output is
Found value: 0 "USE CODE ""Gef,
It should be
Found value: 0 "USE CODE ""Gef sdf"" FROM 1/7/07",
and for the second line Matcher m = r.matcher(line2); the output should be
Found value: 0 "Test asda ds asd",
You may use
(?:"[^"]*(?:""[^"]*)*"|[^,])+
See the regex demo
Explanation:
" - leading quote
[^"]* - 0+ chars other than a double quote
(?:""[^"]*)* - 0+ sequences of a "" text followed with 0+ chars other than a double quote
" - trailing quote
OR:
[^,] - any char but a comma
And the whole pattern is matched 1 or more times as it is enclosed with (?:...)+ and + matches 1 or more occurrences.
IDEONE demo:
String line = "\"USE CODE \"\"Gef, sdf\"\" FROM 1/7/07\", Delete , Hello , How are you ? , ";
String line2 = "Test asda ds asd, tesat2 . test3";
Pattern pattern = Pattern.compile("(?:\"[^\"]*(?:\"\"[^\"]*)*\"|[^,])+");
Matcher matcher = pattern.matcher(line);
if (matcher.find()){ // if is used to get the 1st match only
System.out.println(matcher.group(0));
}
Matcher matcher2 = pattern.matcher(line2);
if (matcher2.find()){
System.out.println(matcher2.group(0));
}
So I have a few lines like such:
tag1:
line1word1 lineoneanychar
line2word1
tag2:
line1word1 ....
line2word1 .....
I am trying to build a java regex that extracts all the data under the tags. i.e:
String parsed1 = line1word1 lineone\nline2word1
String parsed2 = line1word1 ....\nline2word1 .....
I believe the right way to do this is using something like this, but I haven't quite got it right:
Pattern p = Pattern.compile("tag1:\n( {1}.*)\n(?!\\w+)", Pattern.DOTALL);
Matcher m = p.matcher(clean_data);
if(m.find()){
System.out.println(m.group(1));
}
Any help would be appreciated!
Could be something like that
public static void main(String[] args) throws Exception {
String input = "tag1:\n"
+ " line1word1 lineoneanychar\n"
+ " line2word1\n"
+ "tag2:\n"
+ " line1word1 ....\n"
+ " line2word1 .....\n";
Pattern p = Pattern.compile("tag\\d+:$\\n((?:^\\s.*?$\\n)+)", Pattern.DOTALL|Pattern.MULTILINE);
Matcher m = p.matcher(input);
while(m.find()){
System.out.println(m.group(1));
}
}
Remember to escape \\ in your regex.
\d is a number
\s a space
(?:something) is for making a group that won't be a real 'group' in the matcher
How do I write a regex that will match multiline delmitied by new line and spaces?
The following code works for one multiline but does not work if the input
is
String input = "A1234567890\nAAAAA\nwwwwwwww"
By which I mean matches() is not true for the input.
Here is my code:
package patternreg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pattrenmatching {
public static void main(String[] args) {
String input = "A1234567890\nAAAAA";
String regex = ".*[\\w\\s\\w+].*";
Pattern p = Pattern.compile(regex,Pattern.MULTILINE);
Matcher m =p.matcher(input);
if (m.matches()) {
System.out.println("matches() found the pattern \""
+ "\" starting at index "
+ " and ending at index ");
} else {
System.out.println("matches() found nothing");
}
}
}
You could also add the DOTALL flag to get it working:
Pattern p = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
I believe your problem is that .* is greedy, so it's matching all the other '\n' in the string.
If you want to stick with the code above try: "[\S]*[\s]+". Which means match zero or more non-whitespace chars followed by one or more whitespace chars.
fixed up code:
public static void main(String[] args) {
String input = "A1234567890\nAAAAA\nsdfasdf\nasdfasdf";
String regex = "[\\S]*[\\s]+";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(input.substring(m.start(), m.end()) + "*");
}
if (m.matches()) {
System.out.println("matches() found the pattern \"" + "\" starting at index " + " and ending at index ");
} else {
System.out.println("matches() found nothing");
}
}
OUTPUT:
A1234567890
* AAAAA
* sdfasdf
* matches() found nothing
Also, a pattern of
"([\\S]*[\\s]+)+([\\S])*"
will match the entire output (matcher returns true) but messes up the token part of your code.