Regular expression to retreieve integer - java

Hi I have an URL like "/yyyyyy/xm", where x can be an integer denoting the number of minutes. I need to parse and get this value. Any idea of how this can be using regex or String.split() method? The pattern of the URL is always the same like for example:
/magnetic/20m should give me the value 20
/temperature/500m should give me the value 500
Thanks in advance!

The following should work:
/.*?/(\d+)
You just need to access to the 1st group of the match, and you'll get the numbers there.
Edit:
In the future, finding the regex by yourself. That's a pretty straightforward regex question.

And if you don't like regexp...
String txt = "/magnetic/20m";
String[] components = txt.split("/");
String lastComponent = components[components.length - 1];
int result = Integer.parseInt(lastComponent.replace("m", ""));

Related

Java remove dynamic substring from string

I need to remove dynamic substring from string. There is a few similar topic of this theme, but noone of them helped me. I have a string e.g.:
product test1="001" test2="abc" test3="123xzy"
and i need output:
product test1="001" test3="123xzy"
I mean I need remove test2="abc". test2 is an unique element and can be placed anywhere in original string. "abc" is dynamic variable and can have various length. What is the fastest and the most elegant solution of this problem? Thx
You can use a regular expression:
String input = "product test1=\"001\" test2=\"abc\" test3=\"123xzy\"";
String result = input.replaceAll("test2=\".*?\"\\s+", "");
In substance: find a substring like test2="xxxxxx", optionally followed by some spaces (\\s+) and replace it with nothing.

Regex to extract value from link

I have a String like file:///android_asset/GwyXUyisyq. I want to extract the GwyXUyisyq from the rest of the string. The value will change in every instance, but the file:///android_asset/ will always remain fixed. What regex can I use to achieve the same?
You don't need a regex here :
Just find the last index of / and replace everything before it :)
String s = "file:///android_asset/GwyXUyisyq";
System.out.println(s.replace(s.substring(0,s.lastIndexOf("/")+1), ""));
O/P :GwyXUyisyq

issues to split the string in flex?

hi am trying to split the string in flex,but i can't to separate correctly
private var image_path:String = "http://pvm4.yyy.in/sample-31/demo/img0.jpg";
I want to split the number 0
so am trying this code
image_path.substring(image_path.lastIndexOf("/img"));
but am getting img0.jpg i need 0 only how to split this?
image_path.substring(image_path.lastIndexOf("/img")+4, image_path.length-4);
You can do it the same way but by adding the length of your parameter :
(the syntax could not be good as I don't know flex)
image_path.substring(image_path.lastIndexOf("/img") + "/img".length);
But you sould use the following regex to get the number :
.*/demo/img([0-9]+)\.jpg
then use capturing group to get the number ;)

How to get last few characters of varying length from a string ..?

I have a multiple string urls, from which i have to pick last few characters, which are id's infact. But the problem is that, the length of id's is not consistent, i.e., if one id is of length 6 then, other may be of length 5 or 4 and so on. The sample urls are like:
www.abc.com/xyz-123456
www.abc.com/pqr-5432
www.abc.com/lmn/opqr-25647
it could have been a lot easier if the length of the particular id portion would have been same, i could have used:
String abc = "www.abc.com/xyz-123456";
String id = abc.substring(abc.length()-6);
But now the scenario is different as length of id portion in the selected url is not the same always, How can i cater this varying id..???? please any help is appreciated.
There is a lastIndexOf method on the String object that will let you find the position of the '-' (I take it that is your separator). From there you can do the substring.
You can use something like this.
String id=abc.subString(abc.lastIndexOf('\'),abc.length()-1);
Hope it will help you. :)
String url1 = "www.abc.com/xyz-123456";
String[] url1Split = url1.split("-");
What you're looking for can be found in url1split[1]
Use regex to remove all characters upto -.
String id = url.replaceAll("^.*-","");
or
String id = url.replaceAll("^.*-(\\w+)$","$1");
You can use LastIndexOf or create the regular expression.

How do I split a concatenated string into multiple floating point values?

I'm a begginer in java I have
packet=090209153038020734.0090209153039020734.0
like this I want to split this string and store into an array like two strings:
1) 090209153038020734.0
2) 090209153039020734.0
I have done like this:
String packetArray[] = packets.split(packets,Constants.SF);
Where:
Constants.SF=0x01.
But it won't work.
Please help me.
I'd think twice about using split since those are obviously fixed width fields.
I've seen them before on another question here (several in fact so I'm guessing this may be homework (or a popular data collection device :-)) and it's plain that the protocol is:
STX (0x01).
0x0f.
date (YYMMDD or DDMMYY).
time (HHMMSS).
0x02.
value (XXXXXX.X).
0x03.
0x04.
And, given that they're fixed width, you should probably just use substrings to get the information out.
The JavaDoc of String is helpful here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
You have your String packet;
String.indexOf(String) gives you a position of a special substring. your interested in the "." sign. So you write
int position = packet.indexOf(".")+1
+1 becuase you want the trailing decimal too. It will return something 20-ish and will be the last pos of the first number.
Then we use substring
String first = packet.substring(0,position) will give you everything up to the ".0"
String second = packet.substring(position-1) should give you everything starting after the ".0" and up to the end of the string.
Now if you want them explicitely into an array you can just put them there. The code as a whole - I may have some "off by one" -bugs.
int position = packet.indexOf(".")+1
String first = packet.substring(0,position)
String second = packet.substring(position-1)
String[] packetArray = new String[2];
packetArray[0] = first;
packetArray[1] = second;
String packetArray[] = packets.split("\u0001");
should work. You are using
public String[] split(String regex, int limit)
which is doing something else: It makes sure that split() returns an array with at most limit members (1 in this case, so you get what you ask for).
You need to read the Javadocs for the String.split() methods...you are calling the version of String.split() that takes a regular expression and a limit, but you are passing the string itself as the first parameter, which doesn't really make sense.
As Aaron Digulla mentioned, use the other version.
You don't say how you want to do the split. It could be based on a fixed length (number of characters) or you want one decimal place.
If the former you could do packetArray = new String[]{packet.substring(0, 20), packet.substring(21)};
int dotIndex = packets.indexOf('.');
packetArray = new String[]{packet.substring(0, dotIndex+2), packet.substring(dotIndex+2)};
Your solution confuses the regexp with the string.
split uses regular expressions as documented here. Your code seems to be trying to match the whole string Constants.SF = 0x01 times, which doesn't make much sense. If you know what char the boxes are then you can use something like {[^c]+cc} where c is the character of the box (i guess this is 0x01), to match each "packet".
I think you are trying to use it like the .net String.Split(...) function?

Categories