Qualcuno ha un buon codice C# (ed espressioni regolari) che analizzerà una stringa e "collegherà" eventuali URL che potrebbero essere nella stringa?Codice C# per collegare gli URL in una stringa
risposta
E 'un compito abbastanza semplice si può acheive con Regex e un ready-to-go espressione regolare da:
Qualcosa di simile:
var html = Regex.Replace(html, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+" +
"\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?" +
"([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$",
"<a href=\"$1\">$1</a>");
È potrebbe anche essere interessato non solo alla creazione di collegamenti, ma anche alla riduzione degli URL. Ecco un buon articolo su questo argomento:
Vedi anche:
- Regular Expression Workbench a MSDN
- Converting a URL into a Link in C# Using Regular Expressions
- Regex to find URL within text and make them as link
- Regex.Replace Method a MSDN
- The Problem With URLs da Jeff Atwood
- Parsing URLs with Regular Expressions and the Regex Object
- Format URLs in string to HTML Links in C#
- Automatically hyperlink URL and Email in ASP.NET Pages with C#
Ciao. Grande risposta. La maggior parte dei suggerimenti nel tuo post (e link) sembrano funzionare, ma sembrano tutti rompere tutti i link esistenti nel testo che viene valutato. –
VSmith puoi provare diverse espressioni di reg da regixlib.com e trovare quale funziona meglio per te. –
@VSmith: stai implicando che hai una stringa come "ciao there, vedi: http://www.b.com"; e vuoi solo collegare il secondo? –
Non è così facile come si può leggere in questo blog post by Jeff Atwood. È particolarmente difficile individuare dove finisce un URL.
Ad esempio, è la parte finale parentesi del URL o no:
- http : //en.wikipedia.org/wiki/PCTools (CentralPointSoftware)
- un URL tra parentesi (http : //en.wikipedia.org) altro testo
Nel primo caso, le parentesi fanno parte dell'URL. Nel secondo caso non lo sono!
E come puoi vedere dagli URL in questa risposta, non tutti hanno capito bene :) – Ray
Beh, in effetti, non volevo che i due URL venissero linkati. Ma sembra che questo non è supportato. – M4N
La regex di Jeff sembra essere visualizzata male nel mio browser, credo che dovrebbe essere: "\ (? \ Bhttp: // [-A-Za-z0-9 + & @ # /%? = ~ _() | !: ,.;] * [- A-Za-z0-9 + & @ # /% = ~ _() |] " –
protected string Linkify(string SearchText) {
// this will find links like:
// http://www.mysite.com
// as well as any links with other characters directly in front of it like:
// href="http://www.mysite.com"
// you can then use your own logic to determine which links to linkify
Regex regx = new Regex(@"\b(((\S+)?)(@|mailto\:|(news|(ht|f)tp(s?))\://)\S+)\b", RegexOptions.IgnoreCase);
SearchText = SearchText.Replace(" ", " ");
MatchCollection matches = regx.Matches(SearchText);
foreach (Match match in matches) {
if (match.Value.StartsWith("http")) { // if it starts with anything else then dont linkify -- may already be linked!
SearchText = SearchText.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
}
}
return SearchText;
}
Cheers per postare quello :) –
Abbiamo finito per utilizzare qualcosa di molto simile, con una modifica. Abbiamo finito per assicurarci che la sostituzione avvenga solo una volta. Ciò significa che alla fine mancheranno alcuni collegamenti (collegamenti che si verificano più volte) ma rimuoverà la possibilità di collegamenti confusi in due casi: 1) Quando ci sono due collegamenti in cui uno è più dettagliato dell'altro. ad es. "http://google.com http://google.com/reader" 2) Quando c'è un mix di link HTML con collegamenti di testo normale. ad es. "Http://google.com Google" se (input.IndexOf (match.Value) == input.LastIndexOf (match.Value)) { ...} –
bene, dopo un sacco di ricerca su questo, e diversi tentativi di risolvere momenti in cui
- persone entrano in http://www.sitename.com e www.sitename.com nello stesso posto
- correzioni per parenthisis come (http://www.sitename.com) e http://msdn.microsoft.com/en-us/library/aa752574(vs.85).aspx
- gli URL lunghi come: http://www.amazon.com/gp/product/b000ads62g/ref=s9_simz_gw_s3_p74_t1?pf_rd_m=atvpdkikx0der&pf_rd_s=center-2&pf_rd_r=04eezfszazqzs8xfm9yd&pf_rd_t=101&pf_rd_p=470938631&pf_rd_i=507846
stiamo usando questa estensione HtmlHelper ...pensato che avrei condividere e ottenere eventuali commenti:
private static Regex regExHttpLinks = new Regex(@"(?<=\()\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|](?=\))|(?<=(?<wrap>[=~|_#]))\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|](?=\k<wrap>)|\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static string Format(this HtmlHelper htmlHelper, string html)
{
if (string.IsNullOrEmpty(html))
{
return html;
}
html = htmlHelper.Encode(html);
html = html.Replace(Environment.NewLine, "<br />");
// replace periods on numeric values that appear to be valid domain names
var periodReplacement = "[[[replace:period]]]";
html = Regex.Replace(html, @"(?<=\d)\.(?=\d)", periodReplacement);
// create links for matches
var linkMatches = regExHttpLinks.Matches(html);
for (int i = 0; i < linkMatches.Count; i++)
{
var temp = linkMatches[i].ToString();
if (!temp.Contains("://"))
{
temp = "http://" + temp;
}
html = html.Replace(linkMatches[i].ToString(), String.Format("<a href=\"{0}\" title=\"{0}\">{1}</a>", temp.Replace(".", periodReplacement).ToLower(), linkMatches[i].ToString().Replace(".", periodReplacement)));
}
// Clear out period replacement
html = html.Replace(periodReplacement, ".");
return html;
}
hanno trovato seguente espressione regolare http://daringfireball.net/2010/07/improved_regex_for_matching_urls
per me sembra molto buona. La soluzione Jeff Atwood non gestisce molti casi. josefresno mi sembra che gestisca tutti i casi. Ma quando ho cercato di capirlo (in caso di richieste di supporto) il mio cervello è stato bollito.
C'è classe:
public class TextLink
{
#region Properties
public const string BeginPattern = "((http|https)://)?(www.)?";
public const string MiddlePattern = @"([a-z0-9\-]*\.)+[a-z]+(:[0-9]+)?";
public const string EndPattern = @"(/\S*)?";
public static string Pattern { get { return BeginPattern + MiddlePattern + EndPattern; } }
public static string ExactPattern { get { return string.Format("^{0}$", Pattern); } }
public string OriginalInput { get; private set; }
public bool Valid { get; private set; }
private bool _isHttps;
private string _readyLink;
#endregion
#region Constructor
public TextLink(string input)
{
this.OriginalInput = input;
var text = Regex.Replace(input, @"(^\s)|(\s$)", "", RegexOptions.IgnoreCase);
Valid = Regex.IsMatch(text, ExactPattern);
if (Valid)
{
_isHttps = Regex.IsMatch(text, "^https:", RegexOptions.IgnoreCase);
// clear begin:
_readyLink = Regex.Replace(text, BeginPattern, "", RegexOptions.IgnoreCase);
// HTTPS
if (_isHttps)
{
_readyLink = "https://www." + _readyLink;
}
// Default
else
{
_readyLink = "http://www." + _readyLink;
}
}
}
#endregion
#region Methods
public override string ToString()
{
return _readyLink;
}
#endregion
}
utilizzarlo in questo modo:
public static string ReplaceUrls(string input)
{
var result = Regex.Replace(input.ToSafeString(), TextLink.Pattern, match =>
{
var textLink = new TextLink(match.Value);
return textLink.Valid ?
string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", textLink, textLink.OriginalInput) :
textLink.OriginalInput;
});
return result;
}
Casi di test:
[TestMethod]
public void RegexUtil_TextLink_Parsing()
{
Assert.IsTrue(new TextLink("smthing.com").Valid);
Assert.IsTrue(new TextLink("www.smthing.com/").Valid);
Assert.IsTrue(new TextLink("http://smthing.com").Valid);
Assert.IsTrue(new TextLink("http://www.smthing.com").Valid);
Assert.IsTrue(new TextLink("http://www.smthing.com/").Valid);
Assert.IsTrue(new TextLink("http://www.smthing.com/publisher").Valid);
// port
Assert.IsTrue(new TextLink("http://www.smthing.com:80").Valid);
Assert.IsTrue(new TextLink("http://www.smthing.com:80/").Valid);
// https
Assert.IsTrue(new TextLink("https://smthing.com").Valid);
Assert.IsFalse(new TextLink("").Valid);
Assert.IsFalse(new TextLink("smthing.com.").Valid);
Assert.IsFalse(new TextLink("smthing.com-").Valid);
}
[TestMethod]
public void RegexUtil_TextLink_ToString()
{
// default
Assert.AreEqual("http://www.smthing.com", new TextLink("smthing.com").ToString());
Assert.AreEqual("http://www.smthing.com", new TextLink("http://www.smthing.com").ToString());
Assert.AreEqual("http://www.smthing.com/", new TextLink("smthing.com/").ToString());
Assert.AreEqual("https://www.smthing.com", new TextLink("https://www.smthing.com").ToString());
}
Questo metodo funziona bene, comunque le partite su cose come o.context o altre stringhe che hanno un punto in esse. Sarebbe bello forzare .com/.org/.net etc, da qualche parte nella stringa –
Inoltre costringe www, che non è sempre il caso. –
- 1. espressione regolare per trovare gli URL all'interno di una stringa
- 2. Estrarre tutti gli URL all'interno di una stringa in Ruby
- 3. Come collegare STL nel codice C++?
- 4. Il modo più semplice per convertire un URL in un collegamento ipertestuale in una stringa C#?
- 5. Crea URL da una stringa
- 6. C# Dividi una stringa per un'altra stringa
- 7. Come reindirizzare gli URL in base alla stringa di query?
- 8. C++ Data-Url come stringa per il byte const *
- 9. Passaggio di una stringa a codice C in Android NDK
- 10. Creazione stringa URL in .NET
- 11. Sostituire gli elementi multipli stringa in C#
- 12. Come collegare a una libreria statica in C?
- 13. Estrai URL da una stringa utilizzando PHP
- 14. Riscrivendo gli URL in ASP.NET?
- 15. Come posso compilare e collegare il codice C++ con il codice C compilato?
- 16. Inizializza una stringa in C per svuotare la stringa
- 17. Collegare il comportamento nel codice dietro
- 18. Come faccio a URL-escape di una stringa in Mathematica?
- 19. come collegare una libreria statica per iOS
- 20. Ruota una stringa in C++?
- 21. scaricare una stringa JSON in C#
- 22. Il modo migliore per tagliare gli spazi in una stringa
- 23. Dove sono gli esempi di codice per collegare i test a Sauce Connect?
- 24. formato una stringa in C
- 25. Tagliare una stringa in C
- 26. Attraversamento una stringa in C++
- 27. Come rimuovere qualsiasi URL all'interno di una stringa in Python
- 28. Come generare codice a barre da una stringa usando C#?
- 29. Genera URL in MVC dal Codice Dietro
- 30. Come estrarre gli URL da una pagina HTML in Python
Questa sembra essere la domanda con la base di espressione canonica regolare soluzione. Forse qualcuno potrebbe modificare il titolo per aiutare gli utenti a trovarlo? – JasonSmith