Reputation: 61
I need to match string as below:
;
-
occurs, match only upto -
excluding -
For e.g. :
abc;
should return abc
abc-xyz;
should return abc
Pattern.compile("^(?<string>.*?);$");
Using above i can achieve half. but dont know how to change this pattern to achieve the second requirement. How do i change .*?
so that it stops at forst occurance of -
I am not good with regex. Any help would be great.
EDIT
I need to capture it as group. i cant change it since there many other patterns to match and capture. Its only part of it that i have posted.
Code looks something like below.
public static final Pattern findString = Pattern.compile("^(?<string>.*?);$");
if(findString.find())
{
return findString.group("string"); //cant change anything here.
}
Upvotes: 2
Views: 1599
Reputation: 61
I have found the solution without removing groupings.
(?<string>.*?)
matches everything upto next grouping pattern(?:-.*?)?
followed by a non grouping pattern starts with -
and comes zero or once. ;
end character.So putting all together:
public static final Pattern findString = Pattern.compile("^(?<string>.*?)(?:-.*?)?;$");
if(findString.find())
{
return findString.group("string"); //cant change anything here.
}
Upvotes: 0
Reputation: 626806
UPDATE
I see you have an issue in the code since you try to access .group
in the Pattern
object, while you need to use the .group
method of the Matcher
object:
public static String GetTheGroup(String str) {
Pattern findString = Pattern.compile("(?s)^(?<string>.*?)[;-]");
Matcher matcher = findString.matcher(str);
if (matcher.find())
{
return matcher.group("string"); //you have to change something here.
}
else
return "";
}
And call it as
System.out.println(GetTheGroup("abc-xyz;"));
See IDEONE demo
OLD ANSWER
Your ^(?<string>.*?);$
regex only matches 0 or more characters other than a newline from the beginning up to the first ;
that is the last character in the string. I guess it is not what you expect.
You should learn more about using character classes in regex, as you can match 1 symbol from a specified character set that is defined with [...]
.
You can achieve this with a String.split
taking the first element only and a [;-]
regex that matches a ;
or -
literally:
String res = "abc-xyz;".split("[;-]")[0];
System.out.println(res);
Or with replaceAll
with (?s)[;-].*$
regex (that matches the first ;
or -
and then anything up to the end of string:
res = "abc-xyz;".replaceAll("(?s)[;-].*$", "");
System.out.println(res);
See IDEONE demo
Upvotes: 0
Reputation: 174706
Just use a negated char class.
^[^-;]*
ie.
Pattern p = Pattern.compile("^[^-;]*");
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println(m.group());
}
This would match any character at the start but not of -
or ;
, zero or more times.
Upvotes: 6
Reputation: 3354
This should do what you are looking for:
[^-;]*
It matches characters that are not -
or ;
.
Tipp: If you don't feel sure with regular expressions there are great online solutions to test your input, e.g. https://regex101.com/
Upvotes: 0