what is the regular expression which i should use to match a string which starts with SDPCDR
and contains date in the format 20120826 and ends with .asn ?
an example string is SDPCDR_delsdp3a_6091_20120826-042451.asn
This would work:
^SDPCDR\w+(\d{8})-\w+.asn$
"^SDPCDR.*\\d{8}.*\\.asn$"
Pretty generous on the date part, but the string is probably specific enough already to avoid false matches. If you're looking for a substring rather than trying to match the entire string, instead use
"SDPCDR.*?\\d{8}.*?\\.asn"
SDPCR_[a-z_]*[0-9]{8,8}-[a-z_]*\\\\.asn
Related
Firstly sorry for the primitive question, I am wondering how the below method is returning true
Pattern.compile("([0-9]{15})").asPredicate().test("ababx300000055773908")
Please let me know, if i am missing something here.
You have to define the start and the end of the String to match.
your pattern is matching the 15 times numeric in the whole string without considering the location of the pattern.
use regex ^[0-9]{15}$
Pattern.compile("(^[0-9]{15}$)").asPredicate().test("ababx300000055773908");
I get some string from server with known and unknow parts. For example:
<simp>example1</simp><op>example2</op><val>example2</val>
I do not wish to parse XML or any use of parsing. What I wish to do is replace
<op>example2</op>
with empty string ("") which string will look like:
<simp>example1</simp><val>example2</val>
What I know it start with op (in <>) and ends with /op (in <>) but the content (example2) may vary.
Can you give me pointer how accomplish this?
You can use regex. Something like
<op>[A-Za-z0-9]*<\/op>
should match. But you can adapt it so that it fits your requirements better. For example if you know that only certain characters can be shown, you can change it.
Afterwards you can use the String#replaceAll method to remove all matching occurrences with the empty string.
Take a look here to test the regex: https://regex101.com/r/WhPIv4/3
and here to check the replaceAll method that takes the regex and the replacement as a parameter: https://developer.android.com/reference/java/lang/String#replaceall
You can try
str.replace(str.substring(str.indexOf("<op>"),str.indexOf("</op>")+5),"");
To remove all, use replaceAll()
str.replaceAll(str.substring(str.indexOf("<op>"),str.indexOf("</op>")+5),"");
I tried sample,
String str="<simp>example1</simp><op>example2</op><val>example2</val><simp>example1</simp><op>example2</op><val>example2</val><simp>example1</simp><op>example2</op><val>example2</val>";
Log.d("testit", str.replaceAll(str.substring(str.indexOf("<op>"), str.indexOf("</op>") + 5), ""));
And the log output was
D/testit: <simp>example1</simp><val>example2</val><simp>example1</simp><val>example2</val><simp>example1</simp><val>example2</val>
Edit
As #Elsafar said , str.replaceAll("<op>.*?</op>", "") will work.
Use like this:
String str = "<simp>example1</simp><op>example2</op><val>example2</val>";
String garbage = str.substring(str.indexOf("<op>"),str.indexOf("</op>")+5).trim();
String newString = str.replace(garbage,"");
I combined all the answers and eventually used:
st.replaceAll("<op>.*?<\\/op>","");
Thank you all for the help
is it possible to detect the pattern of a String and store it in a variable? so, if I have a String test1234 and highlight 1234 I expect something like \d{4}.
It would require that you find a regular expression that both your highlighted substring and desired replacement match and that is in no way unique. For example, "1234" could match .{4} or \d{4} or even .+ , which is not of a unique length. So, even if you could generate a regular expression from a string, it could happen that it would be the string itself or something you didn't want. Maybe you should rethink the general desired outcome of your program and try to come up with a different way of solving the issue at hand.
Hope that helped. Good luck!
I'm trying to create a reg expression to find a sequence of the char 'y' and a '/' before or after it. But only if nothing is after or before the '/' characters. This is better explained with examples. Basically, I need to remove these from the String.
For example:
'dd/MM/yyyy' becomes 'dd/MM'
'dd/yy/MM' becomes 'dd/MM'
'y/MM/dd' becomes 'MM/dd'
Is this even possible with Regex? And if it isn't, what's the best way to approach this? Just if-then logic?
You can use
String replacement = inputString.replaceAll("/y+|y+/","");
What about replacing: \/y+|y+\/ with nothing?
I am trying to create a suitable regular expression to use java.util.regex.Pattern
I am using the regular expression shown below to match Strings like so: feed_user_at_gmail_dot_com_testfile
final static Pattern PATTERN1 = Pattern.compile("feed_(.*)_([^_]*)");
This works as expected. But, I need to create another Pattern to match Strings like so: feed_user_at_gmail_dot_com_testfile_ts_20120413_dot_175531_dot_463
The difference is that the second String is a time stamped version of the first String. These two Strings are examples of file names in my database and I need to identify them both separately. The time stamped version is appended with _ts_ followed by DATE as shown above. All dots in the DATE are changed to _dot_
Thanks,
Sony
How about this:
"feed_(.*)_([^_]*)_ts_[1-9]+(_dot_[1-9]+)*"
Or better yet,
"feed_(.*)_([^_]*)_ts_[1-9]+(_dot_[1-9]+){2}"
if dates always have exactly two dots.