2016-05-04 11 views
1

私は1つのパラグラフ上の段落で大きなテキストコンテンツから特定のテキストを抽出するにはどうすればよいですか?

Dear testUser testUser, 
You have been registered in the Request Management System. 
Your username is the email address to which this notification is addressed. 
Please click the link below to set your password: http://www.example.com/QA/[email protected]&tempPassword=b0cb6ca7-2e52-4b80-8252-f5dac499cea3 
You will receive further information from the our employee that has registered you in the system. 
Thank you 

を持っている私は、新しいパスワードを生成するために使用のものであるURLを抽出したいです。

私はこれに正規表現を使用しましたが、私はそれを達成することができます。以下は

iは

String REGEX="Dear testuser,testuser,|You have been registered in the Request Management System.|Your username is the email address to which this notification is addressed.|Please click the link below to set your password:|You will receive further information from the our employee that has registered you in the system.|Thank you"; 
     if(copiedText.contains("password")) 
        { 
         copiedText=copiedText.replaceAll(REGEX, "").trim(); 
         System.out.println("Final Text after replace: "+copiedText); 
         keyValues.put(key,copiedText); 
         System.out.println("the Values stored in map are:" + keyValues); 

        } 

を使用したコードである私はこれを使ってURLを取得していますが、私は良い方法ではないとしてこれを検討してください。ユーザー名もリアルタイムのシナリオで変更されるため、汎用ソリューションが必要です。では、これをJavaでどのように処理できるのでしょうか。

+0

私はあなたが必要とするものについてより明確にする必要があると思います。それが機能しているが、それが良いアプローチではないと感じている場合は、あなたの要件が良いアプローチであることをお知らせください。異なるユーザーを扱う場合は、[このチュートリアル](https://docs.oracle.com/javase/tutorial/essential/regex/)を見てください。_character classes_を使用して文字の並び他の種類の文字の)。 – ajb

+0

可能であれば、いくつかの正規表現を使用して、異なるユーザー名を扱いたいと思います。 –

+0

正規表現を投稿できますか?ここでも「キー」は何ですか? – Adi

答えて

0

あなたは、あなたが簡単に必要なURLを取得しますspit string

String expe= "Dear testUser testUser," 
      + "You have been registered in the Request Management System. " 
      + "Your username is the email address to which this notification is addressed. " 
      + "Please click the link below to set your password: http://www.example.com/QA/[email protected]&tempPassword=b0cb6ca7-2e52-4b80-8252-f5dac499cea3" 
      + "You will receive further information from the our employee that has registered you in the system." 
      + "Thank you"; 


     String[] parts = expe.split("your password: "); 
     String part2 = parts[1]; 
     parts = part2.split("You will receive further information"); 
     part2 = parts[0]; 
     System.out.println(part2); 

を使用することができます。

+0

はい、これも使用できます。 –

関連する問題