Please check following code.
Pattern pxPattern = Pattern.compile("^.*[0-9]+(%|pt|em).*$");
Matcher pxMatcher = pxPattern.matcher("onehellot455emwohellothree");
System.out.println(pxMatcher.matches());
System.out.println(pxMatcher.group(0));
i want to substract string 445em. i am using code for checking css. means i wants to extract
just values like 45em or 50%.
Thanks.
First, the captured group is in group 1, not group 0. Then you need to modify your regex to not consume the numbers and include them in the group. Try:
Pattern pxPattern = Pattern.compile("^.*?([0-9]+(?:%|pt|em)).*$");
Matcher pxMatcher = pxPattern.matcher("onehellot455emwohellothree");
System.out.println(pxMatcher.matches());
System.out.println(pxMatcher.group(1));
EDIT:
To get all values from a string with several, you can use this pattern:
Pattern pxPattern = Pattern.compile("[0-9]+(?:%|pt|em)");
Matcher pxMatcher = pxPattern.matcher("margin: 0pt, 6em, 5%, 2pt");
List<String> propertyValues = new ArrayList<String>();
while (pxMatcher.find()) {
propertyValues.add(pxMatcher.group());
}
Related
Below is my string.
{" 3/4", "ERW", "A53-A", "STEEL", "PIPE", "STD", "BLK", "PE"}
i need to match this string using regular expression, please help me to achieve this.
I tried below code snippet to achieve this, but it is matching partially(only 6 strings i can match using this).
String pattern = "\\s*,\\s*";
String[] sourceValues= listTwo.get(1).toString().split(pattern);
i cant able to match first and last string using this pattern.
Please help me to achieve this, i need to match all 8 strings.
Thanks,
Sandesh P
You might try:
" ?([^"]+)"
That would capture what is between double quotes (without a leading single whitespace) in group 1. Now you have 8 strings instead of 6.
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("\" ?([^\"]+)\"").matcher("{\" 3/4\", \"ERW\", \"A53-A\", \"STEEL\", \"PIPE\", \"STD\", \"BLK\", \"PE\"}");
while (m.find()) {
allMatches.add(m.group(1));
}
Java output test
I have a string of the following format
A34B56A12B56
And I am trying to sort the numbers into two arrays based on the prefixes.
For example:
Array A: 34,12
Array B: 56,56
What is the simplest way to go about this?
I have tried to use the String Tokenizer class and I am able to extract the numbers, however there is no way of telling what the prefix was. Essentially, I can only extract them into a single array.
Any help would be appreciated.
Thanks!
Andreas seems to have provided a good answer already, but I wanted to practice some regular expressions in Java, so I wrote the following solution that works for any typical alphabetical prefix: (Comments are in-line.)
String str = "A34B56A12B56";
// pattern that captures the prefix and the suffix groups
String regexStr = "([A-z]+)([0-9]+)";
// compile the regex pattern
Pattern regexPattern = Pattern.compile(regexStr);
// create the matcher
Matcher regexMatcher = regexPattern.matcher(str);
HashMap<String, ArrayList<Long>> prefixToNumsMap = new HashMap<>();
// retrieve all matches, add to prefix bucket
while (regexMatcher.find()) {
// get letter prefix (assuming can be more than one letter for generality)
String prefix = regexMatcher.group(1);
// get number
long suffix = Long.parseLong(regexMatcher.group(2));
// search for list in map
ArrayList<Long> nums = prefixToNumsMap.get(prefix);
// if prefix new, create new list with the number added, update the map
if (nums == null) {
nums = new ArrayList<Long>();
nums.add(suffix);
prefixToNumsMap.put(prefix, nums);
} else { // otherwise add the number to the existing list
nums.add(suffix);
}
System.out.println(prefixToNumsMap);
}
Output : {A=[34, 12], B=[56, 56]}
I have a String which looks like "12:00:00, 2:30:003:45:00,23:45:00".Now my requirement is i want to extract the String before comma and want to put them in a list and another list in another list ,I have done something but that is not working ,I am giving my code
String[] toks = "12:00:00, 2:30:003:45:00,23:45:00".
split("\\s*,\\s*|(?<=[ap]m)(?=\\d)");
for (String tok: toks){
System.out.printf("[%s]%n", tok);
}
But that is giving output as
[12:00:00]
[2:30:003:45:00]
[23:45:00]
My desired result is 12:00:00,3:45:00 in a list and 2:30:00,23:45:00 in a list ,how to achieve that??somebody please help
Here is one way you can do matching and extract these elements in 2 different lists:
Pattern p = Pattern.compile(
"(\\d{1,2}:\\d{1,2}:\\d{1,2})\\s*,\\s*(\\d{1,2}:\\d{1,2}:\\d{1,2})");
Matcher m = p.matcher( "12:00:00, 2:30:003:45:00,23:45:00" );
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
while (m.find()) {
list1.add(m.group(1));
list2.add(m.group(2));
}
System.out.printf("%s%n%s%n", list1, list2);
Output:
[12:00:00, 3:45:00]
[2:30:00, 23:45:00]
There are 2 capturing groups being used here to extract data in 2 different lists:
(\\d{1,2}:\\d{1,2}:\\d{1,2}) # hh:mm:ss timestamp (group #1)
\\s*,\\s* # separator is a comma surrounded by optional spaces
(\\d{1,2}:\\d{1,2}:\\d{1,2}) # hh:mm:ss timestamp (group #2)
What about
final String s = "12:00:00, 2:30:003:45:00,23:45:00";
final Pattern p = Pattern.compile("\\s*(\\d+:\\d\\d:\\d\\d)");
final Matcher m = p.matcher(s);
final List<String> tokens = new ArrayList<>();
while(m.find()) {
tokens.add(m.group(1));
}
for (String tok: tokens){
System.out.printf("[%s]%n", tok);
}
Why not do something like /.\d:.\d:.\d/ to match with a time pattern?
The following code should help you achieve what you want for this case in particular (where input is your string):
Pattern p = Pattern.compile("(\\d{1,2}:\\d{2}:\\d{2})\\s*,\\s*(\\d{1,2}:\\d{1,2}:\\d{1,2})(\\d{1,2}:\\d{2}:\\d{2})\\s*,\\s*(\\d{1,2}:\\d{2}:\\d{2})");
Matcher m = p.matcher(input);
if (m.matches) {
list1.add(m.group(1));
list1.add(m.group(3));
list2.add(m.group(2));
list2.add(m.group(4));
}
This code makes the assumption that those are 4 times, the second and the third have no separator and that the seconds part is always present and has two digits.
For more info on what that pattern means, check Java documentation.
However, you should consider using a more useful string if possible as input. If you can control the output, consider placing a space or any other separator between the two sets and create a SimpleDateFormat to parse it.
Is there some kind of rule engine or some smart way to do this?
I have a string like this :
test 1-2-22
SO that I can get these values:
name = "test"
part_id = 1
brand_id = 2
count = 22
I have more of these so called rules from which I know the format of string.
I was thinking I can do this with regex, but is there a better way of doing this instead?
Edit:
I see some very good answers. Maybe I should have been more clear.
This is not the only string type that I might have, I could have a string like this :
test 3-brand 15 – 2
Where after parsing it should be :
name = "test"
part_id = 2
brand_id = 3
count = 15
So I can have different strings and I need to definy a rule/pattern for each of those. What would be good way to do this? Regex is one option for now
You can split around both spaces and dashes using the following expression:
[ -]
Then you will find the different components at indexes starting from 0.
In Java:
String input = "test 1-2-22";
String[] results = input.split("[ -]");
You can use this Pattern regex:
Pattern pattern = Pattern.compile("^([a-zA-Z]+)\\s*([^-]+)-([^-]+)-([^-]+)$");
Then this code should work:
String line = "test 1-2-22";
Pattern pattern = Pattern.compile("^([a-zA-Z]+)\\s*([^-]+)-([^-]+)-([^-]+)$");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.printf("name:%s, part_id:%s, brand_id:%s, count:%s%n",
matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4) );
}
In this particular case, suitable split operations (or other manual string processing) is probably going to be easiest, as you have the whitespace and the dashes to look for explicitly.
For more complex patterns you can look into antlr for tokenising this into (for example) one identifier and three number tokens and then parsing it, but that seems to be overkill here. (This would give you a 'rule engine', thugh.)
In general: you may want to read up on parsing and context-free grammars for this.
Something like this:
String s = "test 1-2-22";
String[] vars = s.split("[ -]");
String name = vars[0];
String part_id = vars[1];
String brand_id = vars[2];
String count = vars[3];
This will split the string if a space or "-" occurs.
you could then convert the ids and count to int if required.
I need to extract "URPlus1_S2_3" from the string:
"Last one: http://abc.imp/Basic2#URPlus1_S2_3,"
using regular expression in Java language.
Can someone please help me? I am using regex for the first time.
Try
Pattern p = Pattern.compile("#([^,]*)");
Matcher m = p.matcher(myString);
if (m.find()) {
doSomethingWith(m.group(1)); // The matched substring
}
String s = "Last one: http://abc.imp/Basic2#URPlus1_S2_3,";
Matcher m = Pattern.compile("(URPlus1_S2_3)").matcher(s);
if (m.find()) System.out.println(m.group(1));
You gotta learn how to specify your requirements ;)
You haven't really defined what criteria you need to use to find that string, but here is one way to approach based on '#' separator. You can adjust the regex as necessary.
expr: .*#([^,]*)
extract: \1
Go here for syntax documentation:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
String s = Last one: http://abc.imp/Basic2#URPlus1_S2_3,"
String result = s.replaceAll(".*#", "");
The above returns the full String in case there's no "#". There are better ways using regex, but the best solution here is using no regex. There are classes URL and URI doing the job.
Since it's the first time you use regular expressions I would suggest going another way, which is more understandable for now (until you master regular expressions ;) and it will be easily modified if you will ever need to:
String yourPart = new String().split("#")[1];
Here's a long version:
String url = "http://abc.imp/Basic2#URPlus1_S2_3,";
String anchor = null;
String ps = "#(.+),";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(url);
if (m.matches()) {
anchor = m.group(1);
}
The main point to understand is the use of the parenthesis, they are used to create groups which can be extracted from a pattern. In the Matcher object, the group method will return them in order starting at index 1, while the full match is returned by the index 0.
If you just want everything after the #, use split:
String s = "Last one: http://abc.imp/Basic2#URPlus1_S2_3," ;
System.out.println(s.split("#")[1]);
Alternatively, if you want to parse the URI and get the fragment component you can do:
URI u = new URI("http://abc.imp/Basic2#URPlus1_S2_3,");
System.out.println(u.getFragment());