1

I have large database. I want to check my database capitalize errors. I use this pattern for repeated chars. Pattern works but i need to start and end condition with string.

Pattern:

(\w)\1+

Target String:

Javaaa

result: aaa

I want to add condition to regex; Start with Ja and end with a*. Result **only must be repetead characters.

(I dont want to control programmatically only regex do this if its possible

(I'm do this with String.replaceAll(regex, string) not to Pattern or Matcher class)

3
  • you mean this? \\bJa\\w*(\\w)\\1+\\w*a\\b Commented May 9, 2016 at 13:59
  • this regex result select all. This have condition but result must be aaa if target string Prooo and regex \\bPro\\w*(\\w)\\1+\\w*o\\b then result must be ooo. Your regex result word all Commented May 9, 2016 at 14:03
  • capture the repeated part, \\bJa\\w*((\\w)\\2+)\\w*a\\b Commented May 9, 2016 at 14:05

2 Answers 2

2

You may use a lookahead anchored at the leading word boundary:

\b(?=Ja\w*a\b)\w*?((\w)\2+)\w*\b

See the regex demo

Details:

  • \b - leading word boundary
  • (?=Ja\w*a\b) - a positive lookahead that requires the whole word to start with Ja, then it can have 0+ word characters and end with a
  • \w*? - 0+ word characters but as few as possible
  • ((\w)\2+) - Group 1 matching identical consecutive characters
  • \w* - any remaining word characters (0 or more)
  • \b - trailing word boundary.

The result you are seeking is in Group 1.

String s = "Prooo\nJavaaa";
Pattern pattern = Pattern.compile("\\b(?=Ja\\w*a\\b)\\w*?((\\w)\\2+)\\w*\\b");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group(1)); 
} 

See the Java demo.

Sign up to request clarification or add additional context in comments.

7 Comments

I dont want it because i have large capitalize list have many regex and i use String.replaceAll(regex, string) methods. For example in my list: [ ]{1,}[.]{1,}[ ]{1,}###. (### is splitter).
What is the trouble then? Replace with $1. Are the strings standalone? If they are, just use ^ instead of the leading word boundary, and $ instead of a trailing word boundary. See this demo.
Yes standalone. For example i want this; In word Javaaa after regex Java if word Prooo after regex Pro. If word Theee do not the anything. I want to only you make the words I have set via String.replaceAll(regex, string) methods.
Awesome thank you. Finally String s = "Javaaa" String.replaceAll() methods after result is : Java with your regex is it possible?
Well, I am not sure what you need. Please let know what variables are known.
|
1

Another code example (inspired from @Wiktor Stribizew's code ) as per your expected input and output format.

public static void main( String[] args )
{
    String[] input =
        { "Javaaa", "Javaaaaaaaaa", "Javaaaaaaaaaaaaaaaaaa", "Paoooo", "Paoooooooo", "Paooooooooxxxxxxxxx" };
    for ( String str : input )
    {

        System.out.println( "Target String :" + str );
        Pattern pattern = Pattern.compile( "((.)\\2+)" );
        Matcher matcher = pattern.matcher( str );
        while ( matcher.find() )
        {
            System.out.println( "result: " + matcher.group() );
        }
        System.out.println( "---------------------" );
    }
    System.out.println( "Finish" );
}

Output:

Target String :Javaaa
result: aaa
---------------------
Target String :Javaaaaaaaaa
result: aaaaaaaaa
---------------------
Target String :Javaaaaaaaaaaaaaaaaaa
result: aaaaaaaaaaaaaaaaaa
---------------------
Target String :Paoooo
result: oooo
---------------------
Target String :Paoooooooo
result: oooooooo
---------------------
Target String :Paooooooooxxxxxxxxx
result: oooooooo
result: xxxxxxxxx
---------------------
Finish

1 Comment

I dont want it because i have large capitalize list have many regex and i use String.replaceAll(regex, string) methods. For example in my list: [ ]{1,}[.]{1,}[ ]{1,}###. (### is splitter).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.