If I have a string, e.g.
setting=value
How can I remove the '=' and turn that into two separate strings containing 'setting' and 'value' respectively?
Thanks very much!
Two options spring to mind.
The first split()s the String on =:
String[] pieces = s.split("=", 2);
String name = pieces[0];
String value = pieces.length > 1 ? pieces[1] : null;
The second uses regexes directly to parse the String:
Pattern p = Pattern.compile("(.*?)=(.*)");
Matcher m = p.matcher(s);
if (m.matches()) {
String name = m.group(1);
String value = m.group(2);
}
The second gives you more power. For example you can automatically lose white space if you change the pattern to:
Pattern p = Pattern.compile("\\s*(.*?)\\s*=\\s*(.*)\\s*");
You don't need a regular expression for this, just do:
String str = "setting=value";
String[] split = str.split("=");
// str[0] == "setting", str[1] == "value"
You might want to set a limit if value can have an = in it too; see the javadoc
Related
I have the following simple code:
String d = "_|,|\\.";
String s1 = "b,_a_.";
Pattern p = Pattern.compile(d);
String[] ss = p.split(s1);
for (String str : ss){
System.out.println(str.trim());
}
The output gives
b
a
Where does the extra space come from between b and a?
You do not have an extra space, you get an empty element in the resulting array because your regex matches only 1 character, and when there are several characters from the set on end, the string is split at each of those characters.
Thus, you should match as many of those characters in your character class as possible with + (1 or more) quantifier by placing the whole expression into a non-capturing group ((?:_|,|\\.)+), or - better - using a character class [_,.]+:
String d = "(?:_|,|\\.)+"; // Or better: String d = "[_,.]+";
String s1 = "b,_a_.";
Pattern p = Pattern.compile(d);
String[] ss = p.split(s1);
for (String str : ss){
System.out.println(str.trim());
}
See IDEONE demo
While i get puzzled my self, maybe what you want is to change your regex to
String d = "[_,\\.]+";
I have input string as
String str = "IN Param - {Parameter|String}{Parameter|String} Out Param - {Parameter Label|String}{Parameter Label2|String}";
I should able to get
{Parameter|String}{Parameter|String}
from In Param and
{Parameter Label|String}{Parameter Label2|String}
from Out Param.
And again in In Param, I should be able to get Parameter and string. How is it possible in regular expression matching Java?
It is possible through groups
So the regex is:
"\\{(.*?)\\|(.*?)\\}"
Group1 captures Parameter
Group2 captures String
In this regex {(.*?)| says match 0 to n characters that begins with { and ends with | and store the result in group1 excluding { and |..This happens similarly with |(.*?)} but it stores the result in group2..
try it here
Pattern p = Pattern.compile("\\{([^|]+)\\|([^}]+)\\}");
Matcher m = p.matcher(str);
while (m.find()) {
String label = m.group(1);
String value = m.group(2);
// do what you need with them
}
I have a string say 123dance456 which I need to split into two strings containing the first sub-string before the sub-string dance (i.e. 123) and after the sub-string dance (i.e. 456). I need to find them and hold them in separate string variables, say String firstSubString = 123; and String secondSubString = 456;.
Is there any given String method that does just that?
You can use String.split(String regex). Just do something like this:
String s = "123dance456";
String[] split = s.split("dance");
String firstSubString = split[0];
String secondSubString = split[1];
Please note that if "dance" occurs more than once in the original string, split() will split on each occurrence -- that's why the return value is an array.
You can do this:
String str = "123dance456";
String substr = "dance";
String before = str.substring(0, str.indexOf(substr));
String after = str.substring(str.indexOf(substr) + substr.length());
Or
String str = "123dance456";
String substr = "dance";
String[] parts = str.split(substr);
String before = parts[0];
String after = parts[1];
It is noteworthy that the second answer not work if we have more than one occurrence of the substring. To that end, if we only want the first one to be recognized, it would be safer to call split with a limit:
String[] parts = str.split(substr, 2);
which ensures that the returned array has at most two elements. Also, since split will interpret its input as a regular expression we have to be wary of invalid regular expression syntax. As such, I would much rather the first solution, since it works irrespective of the composition of the original substring.
To make the first answer more efficient -- as it is my preferred answer -- then, we would need to remember the position of the substring:
final int position = str.indexOf(substr);
if (position >= 0) {
//if the substring does occur within the string, set the values accordingly
before = str.substring(0, position);
after = str.substring(position + substr.length());
} else {
//otherwise, default to the empty string (or some other value)
before = "";
after = "";
}
It always pays to pay attention to these little edge cases.
If you are using commons-lang see StringUtils.substringAfter()
Easiest is to use the split method as the other answers suggest. You can also use a Matcher and a Pattern for a more general approach:
String str = "123dance456";
String splitter = "dance";
Pattern p = Pattern.compile("(.*?)" + splitter + "(.*)");
Matcher m = p.matcher(str);
if (m.matches()) {
firstSubString = m.group(1); // may be empty
secondSubString = m.group(2); // may be empty
} else {
// splitter not found in str
}
String[] data = new String("123dance456").split("dance");
Using the Scanner is a nice alternative:
Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
if ( scanner.hasNext() )
System.out.println( scanner.next() );
if ( scanner.hasNext() )
System.out.println( scanner.next() );
It is quite convenient to process the tokens as a stream and simply ask via hasNext() if new tokens are available. Also you get nice conversions from the Scanner, even localised, like:
Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
System.out.println( scanner.nextDouble() * scanner.nextDouble() );
I am trying to match a regex pattern in Java, and I have two questions:
Inside the pattern I'm looking for there is a known beginning and then an unknown string that I want to get up until the first occurrence of an &.
there are multiple occurrences of these patterns in the line and I would like to get each occurrence separately.
For example I have this input line:
1234567 100,110,116,129,139,140,144,146 http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All&viewItems=25&subCatView=true ISx20070515x00001a http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=Screen+Refresh+Rate%7C120HZ&sName=View+All&subCatView=true 0 2819357575609397706
And I am interested in these strings:
Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.
Screen+Refresh+Rate%7C120HZ
Assuming the known beginning is filter=**, the regular expression pattern (?:filter=\\*\\*)(.*?)(?:&) should get you what you need. Use Matcher.find() to get all occurrences of the pattern in a given string. Using the test string you provided, the following:
final Pattern p = Pattern.compile("(?:filter=\\*\\*)(.*?)(?:&)");
final Matcher m = p.matcher(testString);
int cnt = 0;
while (m.find()) {
System.out.println(++cnt + ": G1: " + m.group(1));
}
Will output:
1: G1: Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.
2: G1: Screen+Refresh+Rate%7C120HZ**
If i know that I might need other query parameters in the future, I think it'll be more prudent to decode and parse the URL.
String url = URLDecoder.decode("http://www.gold.com/shc/s/c_10153_12605_" +
"Computers+%26+Electronics_Televisions?filter=Screen+Refresh+Rate" +
"%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All&viewItems=25&subCatView=true"
,"utf-8");
Pattern amp = Pattern.compile("&");
Pattern eq = Pattern.compile("=");
Map<String, String> params = new HashMap<String, String>();
String queryString = url.substring(url.indexOf('?') + 1);
for(String param : amp.split(queryString)) {
String[] pair = eq.split(param);
params.put(pair[0], pair[1]);
}
for(Entry<String, String> param : params.entrySet()) {
System.out.format("%s = %s\n", param.getKey(), param.getValue());
}
Output
subCatView = true
viewItems = 25
sName = View All
filter = Screen Refresh Rate|120HZ^Screen Size|37 in. to 42 in.
in your example, there is sometimes a "**" at the end before the "&". but basically, (assuming "filter=" is the start pattern you are looking for) you want something like:
"filter=([^&]+)&"
Using the regular expression (?<=filter=\*{0,2})[^&]*[^&*]+ in java:
Pattern p = Pattern.compile("(?<=filter=\\*{0,2})[^&]*[^&*]+");
String s = "1234567 100,110,116,129,139,140,144,146 http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=**Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All**&viewItems=25&subCatView=true ISx20070515x00001a http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=**Screen+Refresh+Rate%7C120HZ**&sName=View+All&subCatView=true 0 2819357575609397706";
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
EDIT:
Added [^&*]+ to the end of the regex to prevent the ** from being included in the second match.
EDIT2:
Changed regular expression to use lookbehind.
The regex you're looking for is
Screen\+Refresh\+Rate[^&]*
You could use Matcher.find() to find all matches.
are you looking for a string that follows with "filter=" and ignores the first "*" and is end with the first "&".
your can try the following:
String str = "1234567 100,110,116,129,139,140,144,146 http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=**Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All**&viewItems=25&subCatView=true ISx20070515x00001a http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=**Screen+Refresh+Rate%7C120HZ**&sName=View+All&subCatView=true 0 2819357575609397706";
Pattern p = Pattern.compile("filter=(?:\\**)([^&]+?)(?:\\**)&");
Matcher matcher = p.matcher(str);
while(matcher.find()){
System.out.println(matcher.group(1));
}
I have a string
abc.xyz.qweweer.cccc
This is actually going to be a Java package name.
I am trying to find out the last string using reg exp, in the above example cccc is the last String.
Basically I am trying to find out the class name from the package string.
How to find out through Java
Given a string pkg = "abc.xyz.qweweer.cccc" you can solve it like this:
Using indexOf:
int index = pkg.lastIndexOf('.');
String lastPart = index == -1 ? pkg : pkg.substring(index + 1);
Using regular expressions with Matcher:
Matcher m = Pattern.compile("[^.]*$").matcher(pkg);
String lastPart = m.find() ? m.group() : null;
Using split (variation of RMT's answer):
String[] names = pkg.split("\\.");
String lastPart = names[names.length - 1];
Why not just split on the "."
String[] names = packageName.split(".");
String className = names[names.length-1];
Do you really want to use regex? You could do a str.substring (str.lastIndexOf (".") + 1) to get the classname.
Alt1 - Extract desired
String packageName = ...
Matcher m = Pattern.compile("[a-zA-Z]+$").matcher(packageName);
if (m.find()) {
m.group(); // your match
}
Alt2 - Remove undesired
You could also try the somewhat less verbose approach:
String result = packageName.replaceAll(".*\\.", "");
You can use Apache StringUtils substringAfterLast for this.
StringUtils.substringAfterLast("abc.xyz.qweweer.cccc", ".")
Regex is not required for this.