Assuming you want the part between single quotes, use this regular expression with a Matcher:
"'(.*?)'"
Example:
String mydata = "some string with 'the data i want' inside";
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
System.out.println(matcher.group(1));
}
Result:
the data i wantAnswer from Mark Byers on Stack Overflow
Try
Pattern p = Pattern.compile("#([^,]*)");
Matcher m = p.matcher(myString);
if (m.find()) {
doSomethingWith(m.group(1)); // The matched substring
}
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
public static void main(String[] args) {
String abcd1 = "/abcd/";
String abcd2 = "/abcd";
String abcd3 = "abcd/";
String abcd4 = "abcd";
System.out.println(abcd1.replaceAll("(^/)?(/$)?", ""));
System.out.println(abcd2.replaceAll("(^/)?(/$)?", ""));
System.out.println(abcd3.replaceAll("(^/)?(/$)?", ""));
System.out.println(abcd4.replaceAll("(^/)?(/$)?", ""));
}
Will work.
Matches the first (^/)? means match 0 or 1 '/' at the beginning of the string, and (/$)? means match 0 or 1 '/' at the end of the string.
Make the regex "(^/*)?(/*$)?" to support matching multiple '/':
String abcd5 = "//abcd///";
System.out.println(abcd1.replaceAll("(^/*)?(/*$)?", ""));
One more guess: ^\/|\/$ for replace RegEx.
Start by reading the javadocs:
Pattern
Matcher
Build a regexp like this :
.+?(r).*
where r is you regexp.
Java Code
String s;// Your string
String r;// Your regexp
Pattern p = Pattern.compile(String.format(".+?(%s).*",r));
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(1));
}
Note
I assume your regexp will be matched only one time in your string s.
You can use negative look behind to get file name
((?:.(?<!/))+)\"
and below regex to get full path
/(.*)\"
Sample code
public static void main(String[] args) {
Pattern pattern = Pattern.compile("/(.*)\"");
Pattern pattern1 = Pattern.compile("((?:.(?<!/))+)\"");
String matchString = "/slm/attachment/63338424306/Note.jpg\"xxxxxxxx";
Matcher matcher = pattern.matcher(matchString);
String fullString = "";
while (matcher.find()) {
fullString = matcher.group(1);
}
matcher = pattern1.matcher(matchString);
String fileName = "";
while (matcher.find()) {
fileName = matcher.group(1);
}
System.out.println(fullString + " " + fileName);
}
As per your comment taking the string as declared below in my code:
Please clarify if your input string is not like this or I'm missing something.
public static void main(String[] args) {
String str = "xxxxxxxxsrc=\"/slm/attachment/63338424306/Note.jpg\"xxxxxxxx";
String url = null;
// The below pattern will grab string between quotes
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
url = m.group(1);
}
// and this will grab filename from the path(url)
p = Pattern.compile("(?:.(?<!/))+$");
m = p.matcher(url);
while (m.find()) {
System.out.println(m.group());
}
}
Declare a temp variable before the if condition and then append the matched string to that temp variable.
String temp = "";
Pattern pattern = Pattern.compile("Gold(\\s{9})(\\d{4})\\.(\\d{2})");
Matcher matcher = pattern.matcher(str);
if (matcher.find())
{
temp = temp + matcher.group();
}
else {
System.out.println("No string found");
}
If you are interest do it in single line.
String str = "----------------------------------------------------------------------"
+ "Metals Bid Ask Change Low High "
+ "----------------------------------------------------------------------"
+ "Gold 1176.40 1177.40 -8.60 -0.73% 1171.90 1183.90";
String s = str.substring(str.indexOf("Gold"))).replaceAll("(Gold\\s{9}\\d{4}.\\d{2}).*", "$1");
Full example:
private static final Pattern p = Pattern.compile("^([a-zA-Z]+)([0-9]+)(.*)");
public static void main(String[] args) {
// create matcher for pattern p and given string
Matcher m = p.matcher("Testing123Testing");
// if an occurrence if a pattern was found in a given string...
if (m.find()) {
// ...then you can use group() methods.
System.out.println(m.group(0)); // whole matched expression
System.out.println(m.group(1)); // first expression from round brackets (Testing)
System.out.println(m.group(2)); // second one (123)
System.out.println(m.group(3)); // third one (Testing)
}
}
Since you're looking for the first number, you can use such regexp:
^\D+(\d+).*
and m.group(1) will return you the first number. Note that signed numbers can contain a minus sign:
^\D+(-?\d+).*
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex1 {
public static void main(String[]args) {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("hello1234goodboy789very2345");
while(m.find()) {
System.out.println(m.group());
}
}
}
Output:
1234
789
2345