This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Scanner sc = new Scanner(System.in);
String s = sc.nextLine().trim();
s= s.replaceAll("\\++"," + ");
Pattern p = Pattern.compile("[a-zA-Z]+|\\d+|[\\(\\*\\)/%]");
Matcher m = p.matcher(s);
while(m.find()){
String str=m.group();
s= s.replace(m.group()," "+str+" ").trim();
}
String[] str=s.split(" +");
for(String s1:str) System.out.print(s1+",");
if s=22*(7-9 output is: 22,*,(,7,-,9,
if s=12*(4-2 output is:1,2,*,(,4,-,2,
Why digits of 12 got separated in second example?
Help me if someone knows about it
It happens because there are two 2 in the last expression. When you get the last 2, you add spaces around all 2s, so it leads to 12 to be split.
Related
This question already has answers here:
Java Regex group 0
(2 answers)
Closed 4 years ago.
I am trying to write a regex for a string which has a format [digit] [to] [digit] eg. 1 to 5 in which if I find a word "to" from a given string i want to extract the number before and after, I have tried this and it's not working.
Pattern p = Pattern.compile("([0-9]+)\\bto\\b([0-9]+)");
Matcher m = p.matcher("1 to 5");
m.find();
System.out.println(m.group(0));
System.out.println(m.group(1));
System.out.println(m.group(2));
Expected o/p
1
to
5
Consider adding a group for the to part.
Also for the space, you want \\s not \\b:
Pattern p = Pattern.compile("([0-9]+)\\s(to)\\s([0-9]+)");
Matcher m = p.matcher("1 to 5");
m.find();
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
And as said in the comments :
" Group zero denotes the entire pattern"
Is it necessary that you must use regex. If not, you can use String functions.
String s="23 to 34";
String toString="to";
if(s.contains(toString)){
int startIndex=s.indexOf(toString);
int endIndex=startIndex+(toString).length();
String s1=s.substring(0, startIndex); //get the first number
String s2=s.substring(endIndex); //get the second number
System.out.println(s1.trim()); // Removing any whitespaces
System.out.println(toString);
System.out.println(s2.trim();
}
This question already has answers here:
Java Find word in a String
(5 answers)
Find word in random string
(3 answers)
How to find index of whole word in string in java
(1 answer)
Regex to find a specific word in a string in java
(3 answers)
Closed 5 years ago.
I have a message that is of the format:
FixedWord1 variable1 FixedWord2 on FixedWord3 variable2/variable3, variable4 = variable5
I need to extract only variable3 from the above message.
Here is what I tried:
String example = "FixedWord1 variable1 FixedWord2 on FixedWord3 variable2/variable3, variable4 = variable5";
I know that the length of FixedWord3 is 6. So,
example.substring(example.lastIndexOf("FixedWord3") + 6 , example.lastIndexOf(",")); //To get {variable2}/{variable3}
And then,
String requiredString[] = example.split("/", 2); //requiredString[1] would contain {variable3} even if it contains /
Can you suggest a more efficient solution to this problem?
EDIT:
This regex should do the trick.
Pattern pattern = Pattern.compile(".+(Device).+[/]([A-Z].+)[,][ ].+");
Matcher matcher = pattern.matcher(yourstring);
if(matcher.matches())
System.out.println(matcher.group(2));
Assumption to make this work:
Variable2 has no slash '/' followed by upper case letter
Variable3 has no comma ',' followed by space ' '
Since you know that variable2 cannot contain a "/" and you know the length of FixedWord3 then how about this?
String deviceName = example.substring(example.lastIndexOf("Device") + 6, example.lastIndexOf(","));
String lastPart = deviceName.substring(deviceName.indexOf("/") + 1);
System.out.println(deviceName);
System.out.println(lastPart);
Prints:
SJ-ME3600X-185/GigabitEthernet0/4
GigabitEthernet0/4
Regex for the help.
One possible approach is catching the match that's after "{variable2}":
{variable2}\/{([^}]+)}
Then you can use Matcher and Pattern and maybe other tools to make it work in Java.
See here for explanation and live demo.
Using Regex patterns are the efficient way to extract the word from a message in java.
String s = "FixedWord1 {variable1} FixedWord2 on FixedWord3 {variable2}/{variable3}, {variable4} = {variable5}";
Pattern p = Pattern.compile("/(\\{([^}]*)\\})");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
Output {variable3}
This question already has answers here:
Java: removing numeric values from string
(7 answers)
Closed 5 years ago.
This is my string address : London, Jon 2 A
And I want to see in my output see London, Jon
I tried to do this :
String result = chapterNumber.substring(0, chapterNumber.indexOf("1"));
But I have to do 10 times from different number maybe is better way to do this
try this
String str = "London, Jon 2 A";
System.out.println(str.replaceAll("\\d.*",""));
You can use regex with groups to get only the first group (the sequence of non-digit chars until the first digit):
String result = chapterNumber.replaceAll("([^\\d])(\\d.*)", "$1");
String s = "London, Jon 2 A";
Matcher matcher = Pattern.compile("\\d+").matcher(s);
matcher.find();
int i = Integer.valueOf(matcher.group());
System.out.println(s.substring(0,s.indexOf(String.valueOf(i))));
String result = chapterNumber.substring(0, chapterNumber.indexOf(","));
This question already has answers here:
How to extract numbers from a string and get an array of ints?
(13 answers)
Closed 4 years ago.
I have a string like this 3x^2 I want to extract the first and the second number and store them in an array. and if they didn't exist they should be considered as 1.
EDIT :
For example in string x the first and the second number are 1
or in string 3x the second number is 1. I think it should be clear now.
Just get digits with the Regex:
String str = "3x^2";
String pattern = "(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
ArrayList<Integer> numbers = new ArrayList<>();
Find with Matcher all numbers and add them to the ArrayList. Don't forget to convert them to int, because m.group() returns the String.
while (m.find()) {
numbers.add(Integer.parseInt(m.group()));
}
And if your formula doesn't contain the second number, add there your desired default item.
if (numbers.size<2) {
numbers.add(1);
}
Finally print it out with:
for (int i: numbers) {
System.out.print(i + " ");
}
And the output for 3x^2 is 3 2.
And for the 8x it is 8 1.
if the numbers are allways separated by x^, just split the string using this separator
String[] splitted = "3x^2".split("x\\^");
This question already has answers here:
Make String first letter capital in java
(8 answers)
Closed 8 years ago.
I need to convert the first letter of a String into a Capital if it is not already one for part of a project of mine. Can anyone help me please?
Try using this,
String str= "haha";
str.replaceFirst("\\w", str.substring(0, 1).toUpperCase());
Try this
String s = "this is my string";
s.substring(0,1).toUpperCase();
In Java, this replaces every alpha-numeric word (plus underscores) so its first character is uppercase:
Matcher m = Pattern.compile("\\b([a-z])(\\w+)").matcher(str);
StringBuffer bfr = new StringBuffer();
while(m.find()) {
m.appendReplacement(bfr,
m.group(1).toUpperCase() + "$2");
}
m.appendTail(bfr);
It does not alter words that are already uppercased.