Reputation: 13
I'm having trouble getting the other side of this regex.
Stranger Number is %19. Friend Number is %30.
My current regex is this (%[0-9]+)
So when I run this regex. The only highlighted are the %30 and %19.
Strange Number is %19. Friend Number is %30.
I just want it the other way around where %19 and %30 is not the highlighted one and everything else is highlighted.
I have tried. this one [^%](?![0-9])
but im not getting my expected output.
thanks for the help!
Upvotes: 1
Views: 81
Reputation: 11020
For completeness, and those on earlier versions of Java, you can just use the Matcher
and a while
loop to extract strings that match a pattern.
public static void main( String[] args ) {
String test = "Number %1. And number %2 also.";
Matcher matcher = Pattern.compile( "%[\\d]+" ).matcher( test );
ArrayList<String> results = new ArrayList<>();
while( matcher.find() )
results.add( matcher.group() );
System.out.println( results );
}
Upvotes: 1
Reputation: 27205
Depending you what you want to do, you don't have to find an inverted regex for (%[0-9]+)
.
For instance, if you planned to extract all substrings matching the inverted regex, you could use yourString.split("%[0-9]+")
.
If you planned to extract all the %...
by splitting the string with yourString.split(invertedRegex)
you could use a matcher instead.
Code taken from this answer.
String[] matches = Pattern.compile("%[0-9]+").matcher(yourString)
.results().map(MatchResult::group)
.toArray(String[]::new);
Upvotes: 1