This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regex to first occurrence only? [duplicate]
(4 answers)
Closed 4 years ago.
I've tested this regex:
String regex = "[e][#](.)+[$]
the regex works well to identify exponential terms, however it breaks when there are two or more consecutive exponential terms.
I test the regex with the usual code:
while(matcher.find()){
String string = matcher.group();
System.out.println("this one: "+string);
}
When I type the expression:
e#x$ + 3e#x+1$
string equals to (e#(x$+3e#x+1$))
By the way, I added the parentheses inside the while loop. They are necessary for
what I am trying to accomplish.
I want the result of string to be (e(#x$))+3(e(#x+1$)
I know the problem lies in "(.)+ What I think is happening is that the regex
includes the first $, what I need is for it to stop at the first $.
How can I include this logic inside the regex?
thank you
Related
This question already has answers here:
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 4 years ago.
I'm having a whole text in a string and I want to find all belgium cell phone numbers.
So I wrote this piece of code:
Pattern cellPhoneRegex = Pattern.compile("^((\\+|00)32\\s?|0)4(60|[789]\\d)(\\s?\\d{2}){3}$");
List<String> cellPhoneList = new ArrayList<>();
Matcher cellPhoneMatches = cellPhoneRegex.matcher("+32495715511");
while (cellPhoneMatches.find()) {
cellPhoneList.add(cellPhoneMatches.group());
}
System.out.println(cellPhoneList);
Now the thing is that when you run this it matches the phone number.
But when the same number is in a huge text it doesn't find anything.
For this string "Tel: +32495715511" there are no matches.
I don't see why it's not matching.
Exactly what #Thefourthbird said. You're regex is looking for an exact match. As in the text to match has to start with (^ means starts with in this example) and end with ($ means ends with in this example) the phone number matching the regex.
Try using this
var telephone = /\(?s?+?32s?\)?s?[789]d{8,}/;
I’ve not tried it before.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
This piece of code
String Str = new String("${project_loc:A}/Foo.yaml;${project_loc:B}/Bar.yaml");
System.out.println(Str.replaceFirst("[$][{].*[}]/", ""));
prints
Bar.yaml
while what I am trying to achieve is:
Foo.yaml;${project_loc:B}/Bar.yaml
Why is it stopping at second closing braces i.e. }, not at first?
What regex exp can be passed to replaceFirst() to achieve the desired result?
FYI: I tried few things at https://regex101.com/ but couldn't find any success.
Dot-asterisk .* will consume everything, including as many }s as it can find; use [^}]* to stop at the first } instead:
System.out.println(Str.replaceFirst("[$][{][^}]*[}]/", ""));
Demo 1.
You could also use reluctant quantifier .*?:
System.out.println(Str.replaceFirst("[$][{].*?[}]/", ""));
Demo 2.
This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 6 years ago.
From the two urls
https://twitter.com/realDonaldTrump/status/829684271812067328
https://twitter.com/realDonaldTrump/status/829684271812067328/
I want to extract the String 829684271812067328 in Java.
My attempt was
\\/status\\/([\\d]+)
but this does not allow anything before /status or after the digits (/).
Whats the solution to this?
If you just need to extract data (rather than validate the url format), I'd use the following regex :
(\\d+)/?$
And extract the first group of the result.
It matches a non-empty sequence of digits that can be followed by a / and must be found at the end of the matched text.
This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 7 years ago.
i have made this code and i don't know what is the problem with it, why the output print " f "??? even that the string contains the specified characters in the regex
String s="x^2+x-20";
Pattern pattern =
Pattern.compile("([+-][0-9]*)(([a-z A-Z])\\^2)"); //regex
Matcher matcher = pattern.matcher(s);
if(matcher.matches()){
System.out.println("t");
} else {
System.out.println("f");}
Your regex makes [+-] not an optional match but rather required. Use the following:
"([+-]?[0-9]*)(([a-z A-Z])\\^2)" // java syntax
Or as a regex without any java escaping:
([+-]?[0-9]?)(([a-z A-Z])\^2)
Also use Matcher.find rather than Matcher.match to find matches that are not the entire string.
This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 8 years ago.
I am Using Regular Expression to break the string, I am trying to break the string but In reqular Expressions I am missing some format. Can any one please let me know where i went wrong.
String betweenstring="['Sheet 1$'].[DEPTNO] AS [DEPTNO]";
System.out.println("betweenstring: "+betweenstring);
Pattern pattern = Pattern.compile("\\w+[.]\\w+");
Matcher matchers=pattern.matcher(betweenstring);
while(matchers.find())
{
String filtereddata=matchers.group(0);
System.out.println("filtereddata: "+filtereddata);
}
I need to break like this:
['Sheet 1$']
[DEPTNO] AS [DEPTNO]
Given your very specific input, this regex works.
([\w\[\]' $]+)\.([\w\[\]' $]+)
Capture group one is before the period, capture group 2, after. To escape this for a Java string:
Pattern pattern = Pattern.compile("([\\w\\[\\]' $]+(\\.*[\\w\\[\\]' $]+)");
However, it would be much easier to split the string on the literal dot, if this is what you are trying to achieve:
String[] pieces = between.split("\\.");
System.out.println(pieces[0]);
System.out.println(pieces[1]);
Output:
['Sheet 1$']
[DEPTNO] AS [DEPTNO]