Guest User

Untitled

a guest
Jan 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. text = "The mean distance of the Sun from the Earth is approximately 149.6 million kilometers (1 AU), though the distance varies as the Earth moves from perihelion in January to aphelion in July"
  2.  
  3. mean distance of the Sun from the Earth is approximately 149.6 million kilometers (1 AU)
  4.  
  5. public static void print() throws Exception {
  6.  
  7. String s = "The mean distance of the Sun from the Earth is approximately 149.6 million kilometers (1 AU), though the distance varies as the Earth moves from perihelion in January to aphelion in July";
  8. int presize = 7;
  9. int postsize = 7;
  10.  
  11. String term = "Earth";
  12. String[] flds = s.split("[\s]+");
  13.  
  14. int idx = 0;
  15. for (idx = 0; idx < flds.length && !flds[idx].equals(term); idx++)
  16. ;
  17.  
  18. if (idx == flds.length)
  19. throw new Exception("Term not found");
  20.  
  21. int start = idx-presize;
  22. if (start < 0)
  23. start = 0;
  24. int end = idx+postsize;
  25. if (end >= flds.length)
  26. end = flds.length-1;
  27. for (int i = start; i <= end; i++) {
  28. System.out.print(flds[i] + " ");
  29. }
  30. }
  31.  
  32. String text = "The mean distance of the Sun from the Earth is approximately 149.6 million kilometers (1 AU), though the distance varies as the Earth moves from perihelion in January to aphelion in July";
  33. String word = "Earth";
  34. int around=7;
  35. String pattern="([^ ]+ ?){0,"+around+"}"+word+"( ?[^ ]+){0,"+around+"}";
  36. if(pattern!=null){
  37. Matcher m = Pattern.compile(pattern).matcher(text);
  38. if(m.find()){
  39. System.out.println(m.group());
  40. }
  41. }
Add Comment
Please, Sign In to add comment