Regular expression to find a middle of a String in Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a string
1,11,cda74944-1c61-4325-8137-83a4ab50cb81-A00123,Bs000000216,20140204185143.811
Can you provide me regular expression or java string function to get the sub string:
cda74944-1c61-4325-8137-83a4ab50cb81-A00123 ?

You split the string:
String[] array = String.split(",");
This will return an Array of String like this
["1", "11", "cda74944-1c61-4325-8137-83a4ab50cb81-A00123", "Bs000000216", "20140204185143.811"]
You can access the part you want in array[2].

If there is a string like this, use pattern:
/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}-\w{6}/

The pattern is:
[a-f0-9]++(?>-[a-z0-9]++)+

You can split the String
String[] allStrings= String.split(",");
then try to get allStrings[2] //it will give you the desired result cda74944-1c61-4325-8137-83a4ab50cb81-A00123

A few options:
Using length:
String s = yourString.subString(0, yourString.length() >>> 1);
Using split:
String s = yourString.split(",")[2];
Alternatively, if you have something representing physical data that is always represented similarly, it might be smarter to wrap it in a class instead.

Related

ReplaceAll but each replacement is replaced by the current number of occurrence java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 14 days ago.
Improve this question
I would like to use replaceAll to replace the every instance of a certain string but replace it with the current occurrence of that string. For example if my string looked like
'String str = "I really like bananas. bananas bananas bananas!"'
Could I use replaceAll to get to:
I really like 1. 2 3 4!
I'm hoping there is a way to use a lambda function to do this
Thanks
One sollution could be to use Pattern and AtomicInteger.
public String replaceStringWithMatchCount(String input, String stringToCount) {
AtomicInteger counter = new AtomicInteger(1);
return Pattern.compile(stringToCount).matcher(input)
.replaceAll((m) -> String.valueOf(counter.getAndIncrement()));
}
Another alternative would be...not to use lambda:
String inputString = "I really like bananas. bananas bananas bananas!";
int i = 1;
while (inputString.toLowerCase().contains("bananas")) {
inputString = inputString.replaceFirst("(?i)(bananas)", Integer.toString(i++));
}
System.out.println(inputString);
The (?i) within the expression used for the String#replaceFirst() method means to Ignore Letter Case.
When run, the Console output will display:
I really like 1. 2 3 4!

How to manipulate unicode characters in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to replace "\u0023 \u0024 ab"
with:
"\\u0023 \\u0024 ab" to maintain its encoding before storing it in database.
There can me many different values after \u, not just 0023, 0024.
I tried using str.replace("\"\\"); or ("\u","\\u") but it doesn't work in java because it treats \u0023 as one character.
Any suggestions how to achieve this ?
I tried like this
public class test {
public static void main(String args[]) {
String s = "\u0023";
s = s.replace("\\", "\\\\");
System.out.println(s);
}
}
but it is giving following output as:
#
but I am expecting:
\\u0023
As said by the first answer here, you can retrieve the (numerical) unicode value of a character with:
// works up to Unicode 3.0
String hexString = Integer.toHexString(s | 0x10000).substring(1);
Using this number, simply print it out:
System.out.println("\u" + hexString);
(Note: code is untested: give me feedback if it doesn't work)
Hope this helps!
String s = "\\u0023";
s = s.replace("\\", "\\\\");

Extract specific string from String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hii friends I am beginner in java. I am getting string from InputStream in the format like:
aD1bD2cD3dD4eD5fD6g
where D1,D2,D3... are data and small letters a,b,c,d,e,f,g are identifier so I can identify data between a and b and c and so on.
Now the problem is that I am getting more than one data pattern with spaces in between. But I need extract only first data from it. For example, consider data received as
a-674b-26c96d-662e-39f93g a74b-2c96d66e-39f86g a-84b-96c96d562e-99f93g
then I need to extract only first data from this entire data string that is
a-674b-26c96d-662e-39f93g
Please help me.
Use this
String[] params = data.split(" ");
String firstString = params[0];
split() function will split your whole string using space as delimiter, resulting in array of strings. First element of the array is the string you are looking for.

Trying to pull out certain numbers from a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string of numbers which looks like this
["100000100685716-2","603834770-2", "604544970-3"]
can someone help me with a regular expression to match each long (first number before the "-") so i can add it to an array?
The following regex should work: (\d+)-
You do not need a regex here:
Use int pos = str.indexOf('-') to get the location of the dash
Use str.substring(0, pos) to get the initial portion of the string.
If dashes in the input strings are optional, you would need to add a check of pos to be non-negative.
(?:\"\d+)
This should do exactly what you want
String s = "[\"100000100685716-2\",\"603834770-2\", \"604544970-3\"]";
System.out.println(s.split("-")[0].substring(2));

trying to obtain a first name in the path after first back slash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to display the first folder name from the path.
/mnt/sdcard/Videos/lk.jpeg, I want to display mnt string. in java
/mnt/sdcard/Videos/lk.jpeg--> **mnt**
You can split on / and use [1] element from result array.
You can either use regular expressions or you can use String.split(). Note that the split() result should be checked for live usage (e.g. if it has at least two elements).
String desired = "/mnt/sdcard/foo".split("/")[1];
String str = "/mnt/sdcard/Videos/lk.jpeg";
System.out.println(str.split("/")[1]);
Try this out. This is a poor question. But maybe the asker can be a newbie.

Categories