buedi
buedi

Reputation: 1

Negate a regular expression in Java

I need some help find a regular expression matching of a text sting for every char expect the first 10. For example, I used the regex:

.{10} to match the first 10 chars of the text

P53236TT0834691

P53236TT08 34691 --> matching

But I need the negative result as matching (from char 11 to x ) Can someone help me with the right expression?

Upvotes: 0

Views: 250

Answers (3)

npinti
npinti

Reputation: 52185

You could use Groups to match and extract what you need, so the regex would be something like so: ^.{10}(.*)$. This will throw any text coming after the 10th character in a group which you can then access later, as illustrated in this previous SO question.

Upvotes: 2

Wug
Wug

Reputation: 13196

In this specific case, you can use:

String pattern = ".{10}(.*)";

The first capturing group will capture all characters in your search string past the 10th. You can trivially extend this to skip any number of characters.

Upvotes: 2

Joey
Joey

Reputation: 354456

Use a lookbehind:

(?<=^.{10}).*

This will ensure that there are 10 characters before the start of the match and then will match anything until the end of the string.

Upvotes: 3

Related Questions