if (word.matches("^[a-zA-Z_](.)][a-zA-Z_]*$") ) {
System.out.println(word);
}
i need to write method to identify method callings within a class.
eg. A a =new A();
a.call();
i need to find the a.call() form my class.
[a-zA-Z_] matches only one character. Append * or + to match multiple characters.
. matches any character. Escape . to match literal dot.
Try following regular expression:
"[a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*\\(.*?\\)"
Example:
import java.util.regex.*;
class T {
public static void main(String[] args) {
String word = "A a =new A(); a.call();";
Pattern pattern = Pattern.compile("[a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*\\(.*?\\)");
Matcher matcher = pattern.matcher(word);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Related
I am having trouble with regex here.
Say i have this input:
608094.21.1.2014.TELE.&BIG00Z.1.1.GBP
My regex looks like this
(\d\d\d\d\.\d?\d\.\d?\d)|(\d?\d\.\d?\d\.\d?\d?\d\d)
I want to extract the date 21.1.2014 out of the string, but all i get is
8094.21.1
I think my problem here is, that 21.1.2014 starts within the (wrong) match before. Is there a simple way to make the matcher look for the next match not after the end of the match before but one character after the beginning of the match before?
You could use a regex like this:
\d{1,2}\.\d{1,2}\.\d{4}
Working demo
Or shorten it and use:
(\d{1,2}\.){2}\d{4}
If the date is always surrounded by dot:
\.(\d\d\d\d\.\d?\d\.\d?\d|\d?\d\.\d?\d\.\d?\d?\d\d)\.
I hope this will help you.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String x = "608094.21.1.2014.TELE.&BIG00Z.1.1.GBP";
String pattern = "[0-9]{2}.[0-9]{1}.[0-9]{4}";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(x);
if (m.find( )) {
System.out.println("Found value: " + m.group() );
}else {
System.out.println("NO MATCH");
}
}
I am trying to find a match between commas if it contains a specific string.
so far i have ,(.*?myString.?*),
Obviously this finds all the input between the first comma in the entire input and the first comma after the string i want. How do i reference the comma immediately before the string that i want?
Edit: i also want to find the match that occurs after a specific set of characters
ie. occurs after (fooo)
dsfdsdafd,safdsa,gfdsgdtheMatchfdsgfd,dsafdsa,dsfoooafd,safdsa,gfhhhgdtheMatchfhhhfd,dsafdsa
returns gfhhhgdtheMatchfhhhfd, not gfdsgdtheMatchfdsgfd
The following regex should do it :
[^,]+theMatch.*?(?=,)
see regex demo / explanation
Java ( demo )
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class RegEx {
public static void main(String[] args) {
String s = "dsfdsdafd,safdsa,gfdsgdtheMatchfdsgfd,dsafdsa";
String r = "[^,]+theMatch.*?(?=,)";
Pattern p = Pattern.compile(r);
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group()); // gfdsgdtheMatchfdsgfd
}
}
}
Edit
use this regex fooo.*?([^,]+theMatch.*?)(?=,) demo
You are finding too much because .* will include the comma.
You need the following regular expression: ,([^,]*myinput[^,]*),
[^,]* basically says find all non-comma characters.
I would suggest the following code:
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String str = "dsfdsdafd,safdsa,myinput,dsafdsa";
Pattern p = Pattern.compile(",([^,]*myinput[^,]*),");
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(m.group(0));
// prints out ",myinput,"
System.out.println(m.group(1));
// prints out "myinput"
}
}
}
Here is a StackOverflow question that is basically the same with some very good answers associated:
Regex to find internal match between two characters
For more on regular expressions in Java look here: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
If you want the position of the comma proceeding your input string use the following code:
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String str = "dsfdsdafd,safdsa,myinput,dsafdsa";
Pattern p = Pattern.compile(",([^,]*myinput[^,]*),");
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(str.indexOf(m.group(0)));
// prints out "16"
}
}
}
By feeding the match of the regular expression into the String Method indexOf( you are able to locate the position of the start of your string.
Edit:
To find the occurrence of a string following another string, simply modify the regex to: fooo.*,([^,]*theMatch[^,]*),
fooo.* will greedily consume all characters between fooo and the start of your match.
Example code:
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String str = "dsfdsdafd,safdsa,gfdsgdtheMatchfdsgfd,dsafdsa,dsfoooafd,safdsa,gfhhhgdtheMatchfhhhfd,dsafdsa";
Pattern p = Pattern.compile("fooo.*,([^,]*theMatch[^,]*),");
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(m.group(1));
// prints out: gfhhhgdtheMatchfhhhfd
}
}
}
The usual approach is to use a pattern that cannot match your delimiter in place of .. In this case, you need that only at the front of the pattern; you can use a reluctant quantifier at the back as you already do (though you've misspelled it). For example:
,([^,]*myString.*?),
I have a String string that contains a newline (\n). When I try to match it with a regular expression pattern it returns false, although there should be a match.
package com.stackoverflow;
public class ExgExTest {
public static void main(String[] args) {
String pattern = ".*[0-9]{2}[A-Z]{2}.*";
String string = "123ABC\nDEF";
if (string.matches(pattern)) {
System.out.println("Matches.");
} else {
System.out.println("Does not match.");
}
} // END: main()
} // END: class
How can I match multiline strings with a regular expression?
How can I match multiline strings with a regular expression?
You need to use DOTALL (s) flag for this:
String pattern = "(?s).*[0-9]{2}[A-Z]{2}.*";
Take note of (?s) which will make DOT match new lines also.
You should use Pattern.quote(pattern) to escape all special characters in the pattern.
Documentation.
Hi and happy new year to all of you,
I have a String like "3-5;9-13;15;20-49".
I want to use a RegEx to:
first, check as far as possible, if the syntax is correct... meaning:
the ranges are separated by a semicolon and a range is defined by x-y or just x (where
x and y are decimals) (the order of the ranges or x
I think I must have some mistake(s) in my RegEx, because matches() gives false although
the string is syntactically correct and find() or group() give me the given ranges...??
If I alter the RegEx slightly...I get matches() to return true, but I canĀ“t get the
"ranges" with find() or group().
What am I doing wrong?
regards,
yves
the code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RangeRegEx {
public static void main(String[] args) {
String rangeExample = "1-5;7;12-22;50-56;60-90;95;";
String rangeRegEx = "(\\d+(-\\d+)?)"; // matches returnes false, but find() and group() return correct groups
System.out.println ("Variant 1 \""+printRegEx(rangeRegEx)+"\":\n--------------------\n");
Pattern mrPattern = Pattern.compile(rangeRegEx);
Matcher m = mrPattern.matcher(rangeExample);
System.out.println ("RegEx matches() -> "+m.matches());
m.reset();
while (m.find()) {
System.out.println(m.group());
}
rangeRegEx = "((\\d+)(-\\d+)?(;)?)+"; // matches() returns true, but find() and group() do not work...
System.out.println ("\n\nVariant 2 \""+printRegEx(rangeRegEx)+"\":\n--------------------\n");
mrPattern = Pattern.compile(rangeRegEx);
m = mrPattern.matcher(rangeExample);
System.out.println ("RegEx matches() -> "+m.matches());
m.reset();
while (m.find()) {
System.out.println(m.group());
}
}
public static String printRegEx(String regEx){
StringBuffer s=new StringBuffer();
for (int c=0;c<regEx.length();c++){
char ch=regEx.charAt(c);
s.append(ch);
if (ch == '\\') s.append("\\");
}
return s.toString();
}
}
Running the code given above prints the following message to the console:
Variant 1 "(\\d+(-\\d+)?)":
--------------------
RegEx matches() -> false
1-5
7
12-22
50-56
60-90
95
Variant 2 "((\\d+)(-\\d+)?(;)?)+":
--------------------
RegEx matches() -> true
1-5;7;12-22;50-56;60-90;95;
Try this one.
(\\d+-\\d+|\\d+)(;(\\d+-\\d+|\\d+))*;?
This defines the following:
1) range is either a number or number-number
2) number is any sequence of digits
3) you have >= 1 ranges, each two consecutive ones are separated by exactly one ;
4) you may have but also may not have ; at the end
Note that 0010-0001 will also be considered a valid range by my regexp.
Based on your comment below: here is what you're trying to achieve.
You need another/simpler regexp for this.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RangeRegEx {
public static void main(String[] args) {
String rangeExample = "1-5;7;12-22;50-56;60-90;95";
String rangeRegEx = "(\\d+-\\d+|\\d+)";
Pattern mrPattern = Pattern.compile(rangeRegEx);
Matcher m = mrPattern.matcher(rangeExample);
while (m.find()) {
System.out.println(m.group(1));
System.out.println("===");
}
}
}
Another example.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RangeRegEx {
public static void main(String[] args) {
String rangeExample = "1-5;7;12-22;50-56;60-90;95;";
String rangeRegEx = "(\\d+-\\d+|\\d+);(\\d+-\\d+|\\d+);(\\d+-\\d+|\\d+);(\\d+-\\d+|\\d+);(\\d+-\\d+|\\d+);(\\d+-\\d+|\\d+);";
Pattern mrPattern = Pattern.compile(rangeRegEx);
Matcher m = mrPattern.matcher(rangeExample);
System.out.println(m.matches());
m.reset();
while (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
System.out.println(m.group(4));
System.out.println(m.group(5));
System.out.println(m.group(6));
}
}
}
The matches() method (doc) tries to match the whole input. This is why first .matches() returns false with pattern (\\d+(-\\d+)?) that is made to look for a single range. A call to group() or group(0) returns the whole match if any.
The find() method (doc) tries to match something in the beginning but it does not need to match the whole input, so it does return true with the same pattern and same input.
To find different matches of the same pattern you need to use find() in a loop. Calling match() is useless here.
Remember groups are numbered as encountered starting from the left.
mrPattern = Pattern.compile("((\\d+)(-\\d+)?)(;)?");
m = mrPattern.matcher("1-5;7;12-22;50-56;60-90;95;");
while (m.find()) {
System.out.println(m.group(1));
}
This should capture the ranges into groups. If you don't want that, I will change it.
(\d+-\d+|\d+);?
I tested it on the following strings, all of which match:
3-5;
3-5;9-13;
3-5;9-13;15;
3-5;9-13;15;20-49
If you want to validate the entire string before getting the groups, you can use a positive lookahead like this:
(?=validation_regex)(capture group)
For example:
(?=(?:(?:\d+-\d+|\d+);?)+)(\d+-\d+|\d+);?
The same mechanism is used in password validation.
I need help with this matter. Look at the following regex:
Pattern pattern = Pattern.compile("[A-Za-z]+(\\-[A-Za-z]+)");
Matcher matcher = pattern.matcher(s1);
I want to look for words like this: "home-made", "aaaa-bbb" and not "aaa - bbb", but not
"aaa--aa--aaa". Basically, I want the following:
word - hyphen - word.
It is working for everything, except this pattern will pass: "aaa--aaa--aaa" and shouldn't. What regex will work for this pattern?
Can can remove the backslash from your expression:
"[A-Za-z]+-[A-Za-z]+"
The following code should work then
Pattern pattern = Pattern.compile("[A-Za-z]+-[A-Za-z]+");
Matcher matcher = pattern.matcher("aaa-bbb");
match = matcher.matches();
Note that you can use Matcher.matches() instead of Matcher.find() in order to check the complete string for a match.
If instead you want to look inside a string using Matcher.find() you can use the expression
"(^|\\s)[A-Za-z]+-[A-Za-z]+(\\s|$)"
but note that then only words separated by whitespace will be found (i.e. no words like aaa-bbb.). To capture also this case you can then use lookbehinds and lookaheads:
"(?<![A-Za-z-])[A-Za-z]+-[A-Za-z]+(?![A-Za-z-])"
which will read
(?<![A-Za-z-]) // before the match there must not be and A-Z or -
[A-Za-z]+ // the match itself consists of one or more A-Z
- // followed by a -
[A-Za-z]+ // followed by one or more A-Z
(?![A-Za-z-]) // but afterwards not by any A-Z or -
An example:
Pattern pattern = Pattern.compile("(?<![A-Za-z-])[A-Za-z]+-[A-Za-z]+(?![A-Za-z-])");
Matcher matcher = pattern.matcher("It is home-made.");
if (matcher.find()) {
System.out.println(matcher.group()); // => home-made
}
Actually I can't reproduce the problem mentioned with your expression, if I use single words in the String. As cleared up with the discussion in the comments though, the String s contains a whole sentence to be first tokenised in words and then matched or not.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp {
private static void match(String s) {
Pattern pattern = Pattern.compile("[A-Za-z]+(\\-[A-Za-z]+)");
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
System.out.println("'" + s + "' match");
} else {
System.out.println("'" + s + "' doesn't match");
}
}
/**
* #param args
*/
public static void main(String[] args) {
match(" -home-made");
match("home-made");
match("aaaa-bbb");
match("aaa - bbb");
match("aaa--aa--aaa");
match("home--home-home");
}
}
The output is:
' -home-made' doesn't match
'home-made' match
'aaaa-bbb' match
'aaa - bbb' doesn't match
'aaa--aa--aaa' doesn't match
'home--home-home' doesn't match