My content is a string like this:whoad123##${studentA.math1.math2}dsafddasfd${studentB.math2.math3},now I want to extract the content studentA,studentB which is in the braces and before the first pot(${**}).What's wrong with my code?
static java.util.regex.Pattern p1=java.util.regex.Pattern.compile("\\*\\$\\{\\w+\\}");
private static String getStudentName(String expression) {
StringBuffer stringBuffer = new StringBuffer();
java.util.regex.Matcher m1= p1.matcher(expression);
while(m1.find()) {
String param=m1.group(1);
stringBuffer.append(param.substring(2,param.indexOf("\\.")) + ",");
}
if(stringBuffer.length()>0){
return stringBuffer.deleteCharAt(stringBuffer.length()-1).toString();
}
return null;
}
Use
\$\{([^{}.]+)
See proof
Declare in Java as
static java.util.regex.Pattern p1=java.util.regex.Pattern.compile("\\$\\{([^{}.]+)");
EXPLANATION
EXPLANATION
--------------------------------------------------------------------------------
\$ '$'
--------------------------------------------------------------------------------
\{ '{'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^{}.]+ any character except: '{', '}', '.' (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
public static String getStudentName(String expression) {
Pattern pattern = Pattern.compile("\\{(?<student>\\w+)\\.[^\\}]+\\}");
Matcher matcher = pattern.matcher(expression);
List<String> names = new ArrayList<>();
while (matcher.find()) {
names.add(matcher.group("student"));
}
return String.join(",", names);
}
See demo in regex101.com
Related
String s = "My cake should have ( sixteen | sixten | six teen ) candles, I love and ( should be | would be ) puff them."
Final changed string
My cake should have <div><p id="1">sixteen</p><p id="2">sixten</p><p id="3">six teen</p></div> candles, I love and <div><p id="1">should be</p><p id="2"> would be</p> puff them
I have tried using this:
Pattern pattern = Pattern.compile("\\|\\s*(.*?)(?=\\s*\\|)");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group(1));
}
Use
import java.util.regex.*;
class Program
{
public static void main (String[] args) throws java.lang.Exception
{
String s = "My cake should have ( sixteen | sixten | six teen ) candles, I love and ( should be | would be ) puff them.";
Pattern pattern = Pattern.compile("\\(([^()|]*\\|[^()]*)\\)");
Matcher matcher = pattern.matcher(s);
StringBuffer changed = new StringBuffer();
while (matcher.find()){
String temp = "<div>";
String[] items = matcher.group(1).trim().split("\\s*\\|\\s*");
for (int i = 1; i<=items.length; i++) {
temp += "<p id=\"" + i + "\">" + items[i-1] + "</p>";
}
matcher.appendReplacement(changed, temp+"</div>");
}
matcher.appendTail(changed);
System.out.println(changed.toString());
}
}
See Java proof.
Results: My cake should have <div><p id="1">sixteen</p><p id="2">sixten</p><p id="3">six teen</p></div> candles, I love and <div><p id="1">should be</p><p id="2">would be</p></div> puff them.
Regex used
\(([^()|]*\|[^()]*)\)
EXPLANATION
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^()|]* any character except: '(', ')', '|' (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\| '|'
--------------------------------------------------------------------------------
[^()]* any character except: '(', ')' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\) ')'
In short: Match parens with pipes, take the content with no parens and split with pipes, trim, combine into one string with loop. StringBuffer and matcher.appendReplacement do the magic with string manipulation during replacing.
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]*";
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
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.