I would like to use the "matches" method of the String class.
I don't want to create a Pattern and Matcher object and use matcher.find()
to match a specific string I am working with.
Here's my code:
String string = "-12Log";
if(string.matches("-?\\d+(\\.\\d*)?[Log]")System.out.println("dinos");
I have used different types of regexes with no success.
I have used the following:
-?\\d+(\\.\\d*)?\\[Log]
-?\\d+(\\.\\d*)?[+a-zA-Z]
-?\\d+(\\.\\d*)?+[a-zA-Z]
Please note that I don't want to break down the string into its characters. I would like to use the string as it is.
Any ideas would be appreciated
thanks for the help everyone: found an answer already
the matcher that worked is:
-?\\d+(\\.\\d*)?.*(Log).*
Related
Is it possible to split a string using a string as delimiter? If yes, how?
Example :
String myString = "hello_world<;>goodbye<;>foo";
myString.split("<;>");
//returns "hello_world", "goodbye" and "foo"
The example in your question works exactly, however this may be coincidental. Keep in mind that the String.split(...) method accepts a RegEx parameter, not a String delimiter.
Check out the RegEx documentation here: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum
yes, exactly as you have in your code.
I am doing a simple pattern matching, which is not working. Please help
The string is:
The number *TER8347834SC* has problems.
The String contains a number TER8347834SC which may change with different messages, so i need to use regex to match this number while comparing the String. So while comparing String I am using the regex as [A-Z0-0] for TER8347834SC which doesn't match.
I know this is quite simple, but i tried many times, please help me in this.
Think you mean this,
"\\b[A-Z0-9]+\\b"
Note that \\b word boundary is a much needed one.
Try using this one:
([A-Z]+[0-9]+)
Your pattern should be like this ONLY if the message is always the same as you mentioned:
String pattern = "The number (.*) has problems.";
I have an arraylist links. All links having same format abc.([a-z]*)/\\d{4}/
List<String > links= new ArrayList<>();
links.add("abc.com/2012/aa");
links.add("abc.com/2014/dddd");
links.add("abc.in/2012/aa");
I need to get the last portion of every link. ie, the part after domain name. Domain name can be anything(.com, .in, .edu etc).
/2012/aa
/2014/dddd
/2012/aa
This is the output i want. How can i get this using regex?
Thanks
Some people, when confronted with a problem, think “I know, I'll use
regular expressions.” Now they have two problems.
(see here for background)
Why use regex ? Perhaps a simpler solution is to use String.split("/") , which gives you an array of substrings of the original string, split by /. See this question for more info.
Note that String.split() does in fact take a regex to determine the boundaries upon which to split. However you don't need a regex in this case and a simple character specification is sufficient.
Try with below regex and use regex grouping feature that is grouped based on parenthesis ().
\.[a-zA-Z]{2,3}(/.*)
Pattern description :
dot followed by two or three letters followed by forward slash then any characters
DEMO
Sample code:
Pattern pattern = Pattern.compile("\\.[a-zA-Z]{2,3}(/.*)");
Matcher matcher = pattern.matcher("abc.com/2012/aa");
if (matcher.find()) {
System.out.println(matcher.group(1));
}
output:
/2012/aa
Note:
You can make it more precise by using \\.[a-zA-Z]{2,3}(/\\d{4}/.*) if there are always 4 digits in the pattern.
String result = s.replaceAll("^[^/]*","");
s would be the string in your list.
Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.
Why not just use the URI class?
output = new URI(link).getPath()
Try this one and use the second capturing group
(.*?)(/.*)
Use foreach loop to iterate over list.
Use substring and indexOf('/').
FOR EXAMPLE
String s="abc.com/2014/dddd";
System.out.println(s.substring(s.indexOf('/')));
OUTPUT
/2014/dddd
Or you can go for split method.
System.out.println(s.split("/",2)[1]);//OUTPUT:2014/dddd --->you need to add /
I want to strip out every occurrence of (title) from a string like below. How do I write a regex for that? I tried a regex like below but it doesn't work.
String ruler1="115.28(54)(title) is renumbered 115.363(title) and amended to read:";
Pattern rulerPattern1 = Pattern.compile("(.*)\\(title\\)(.*)", Pattern.MULTILINE);
System.out.println(rulerPattern1.matcher(ruler1).replaceAll(""));
The regex is much simpler than that - all you need is to escape parentheses, like this:
\\(title\\)
You do not need to use the Pattern class explicitly, because replaceAll takes a regular expression.
String ruler1="115.28(54)(title) is renumbered 115.363(title) and amended to read:";
String result = ruler1.replaceAll("\\(title\\)", "");
Your pattern replaces everything in a string that contains "(title)"
Here is a demo on ideone.
Just use what String has to offer:
System.out.println(ruler1.replace("(title)", ""));
DO NOT be fooled by its name vs .replaceAll(), it is very misleading:
.replace() does NOT use regexes;
.replace() DOES replace all occurrences.
Given what you need to do, it is a perfect fit. Javadoc for .replace()
I don't think regex is a great solution for something so simple. Try StringUtils.replace() from the Apache commons-lang package.
String result = StringUtils.replace(ruler1,"(title)","");
String.split(String regex) splits the string around a given regular expression and returns an String array. But I am interested in the regex matches and would like them to be returned as string array instead of strings around them.
For example,
In case of trival regex like ":" it probably wouldn't matter. But there are regexes which would match a particular date in a paragraph and I would like to get all these dates which may be different each time. I checked the jdk api but couldn't find any such methods. Is there any method that I can make use of?. Any help would much appreciated.
Take a look at java.util.regex package Matcher and Pattern classes:
http://download.oracle.com/javase/6/docs/api/java/util/regex/package-summary.html
Just use the Java regular expression API
Pattern pat = Pattern.compile("\\d");
Matcher mat= pat.matcher("Foo99Bar66Baz");
while(mat.find()) {
System.out.println(mat.group());
}
You can find simple but quite comprehensive examples for startup in the following link
http://www.vogella.de/articles/JavaRegularExpressions/article.html
Also Pattern and Matcher usage example in:
http://www.vogella.de/articles/JavaRegularExpressions/article.html#regexjava