Hello.

There was such a question, the site is processing long words

$message = preg_replace('/([^\s]{55})/' , "$1 ", $message); 

But if you insert a link in the field, say, 60 characters, you get something like this:

href = "link

And it will also become with a space, therefore, it will become inoperative. How can this be corrected?

  • one
    Something like this: / ((?: ^ | \ S) (!? Https?: \ / \ /) [^ \ S] {55}) / - ReinRaus
  • > How can this be corrected? Parse only the contents of the tags, regular or parser. - etki
  • @ReinRaus, did not work - singmen
  • one
    @singmen, have you tried? Here is an example of how to cut lines longer than 10 characters: (((?: \ S | \ G) (?! Https?: \ / \ /) [^ \ S] {10}) regex101.com/r/mP6eD1/1 It is necessary to look at the groups that match in the right column. Unfortunately, this regex site does not allow replay. It would be clearer. -------------- Give the target text on which the regular expression did not work. - ReinRaus
  • @ReinRaus, yes, the problem was in me, the link was sent to the base after post'a and immediately converted to clickable, well, and then when I was “pulling” out of the base, I applied a regular schedule, and it looked like href = "http it did not work, corrected it - it worked, thanks =) - singmen

1 answer 1

 String[] splitted = img.toString().split("http://"); //so index 1 is the url and we can just append http:// back on the beginning to get the whole url String url1 = "http://" + splitted[1]; 

Use this method, read about it, I think it will help. You cut the link before the space (unless, of course, you know approximately what will be there) and after the space. Then glue with the same method.

  • Split method - djbolya September
  • let's say you trim up to 150 characters. From the cropped text, find links, if one is found and does not end in / a. then determine the position of its end. Crop the text before it. Find links using reg. vyvvzheny, a bunch of examples in the network - Jony
  • one
    @djbolya, you are not confused by the absence of any hint of C # in the question? - etki