Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从字符串中删除动态字符字符串?_Android_Regex_String_Replace_Character - Fatal编程技术网

Android 如何从字符串中删除动态字符字符串?

Android 如何从字符串中删除动态字符字符串?,android,regex,string,replace,character,Android,Regex,String,Replace,Character,我是android开发的新手,我正在尝试从字符串中删除一个动态字符字符串。我的字符串: "Beginning of String....&lt;img src="http://webaddress.com" height="1" width="1"/&gt;" 我想删除“<”、“>”以及它们之间的所有内容。我想要的只是“字符串的开头…”到目前为止,我已经尝试过了,但没有成功 description = description.replaceFirst("(?s)(&am

我是android开发的新手,我正在尝试从字符串中删除一个动态字符字符串。我的字符串:

"Beginning of String....&lt;img src="http://webaddress.com" height="1" width="1"/&gt;"
我想删除“<”、“>”以及它们之间的所有内容。我想要的只是“字符串的开头…”到目前为止,我已经尝试过了,但没有成功

description = description.replaceFirst("(?s)(&lt)(.*?)(&gt)","$1$3");
我也在一根类似的绳子上试过,效果很好,所以我不明白我做错了什么

description = description.replaceFirst("(?s)(<sub>)(.*?)(</sub>)","$1$3");
description=description.replaceFirst((?s)(*?),“$1$3”);
我的班级

public class RssReader {

private final static String BOLD_OPEN = "<B>";
private final static String BOLD_CLOSE = "</B>";
private final static String BREAK = "<BR>";
private final static String ITALIC_OPEN = "<I>";
private final static String ITALIC_CLOSE = "</I>";
private final static String SMALL_OPEN = "<SMALL>";
private final static String SMALL_CLOSE = "</SMALL>";


public static List<JSONObject> getLatestRssFeed(){
    String feed = "http://feeds.feedburner.com/MetalMarketCommentary";
    //http://globoesporte.globo.com/dynamo/futebol/times/vasco/rss2.xml
    //http://feeds.feedburner.com/GoldMoneyGoldResearch +
    //http://feeds.feedburner.com/GoldsilvercomNews +
    //http://feed43.com/7466558277232702.xml
    //http://feeds.feedburner.com/SilverGoldDaily
    //http://feeds.feedburner.com/MetalMarketCommentary
    //http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&bclid=1644543007001&bctid=1854182861001

    RSSHandler rh = new RSSHandler();
    List<Article> articles =  rh.getLatestArticles(feed);
    Log.e("RSS ERROR", "Number of articles " + articles.size());
    return fillData(articles);
}



private static List<JSONObject> fillData(List<Article> articles) {

    List<JSONObject> items = new ArrayList<JSONObject>();
    for (Article article : articles) {
        JSONObject current = new JSONObject();
        try {
            buildJsonObject(article, current);
        } catch (JSONException e) {
            Log.e("RSS ERROR", "Error creating JSON Object from RSS feed");
        }
        items.add(current);
    }

    return items;
}



private static void buildJsonObject(Article article, JSONObject current) throws JSONException {
    String title = article.getTitle();
    String description = article.getDescription();
    description = description.replaceFirst("(?s)(<sub>)(.*?)(</sub>)","$1$3");
    int start = description.indexOf(".&");
    description= description.substring(0, start);
    String date = article.getPubDate();
    String imgLink = article.getImgLink();

    StringBuffer sb = new StringBuffer();
    sb.append(BOLD_OPEN).append(title).append(BOLD_CLOSE);
    sb.append(BREAK);
    sb.append(description);
    sb.append(BREAK);
    sb.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE);

    current.put("text", Html.fromHtml(sb.toString()));
    current.put("imageLink", imgLink);
}
}
公共类RssReader{
私有最终静态字符串BOLD_OPEN=“”;
私有最终静态字符串BOLD_CLOSE=“”;
私有最终静态字符串中断=“
”; 私有最终静态字符串ITALIC_OPEN=“”; 私有最终静态字符串ITALIC_CLOSE=“”; 私有最终静态字符串SMALL_OPEN=“”; 私有最终静态字符串小_CLOSE=“”; 公共静态列表getLatestRssFeed(){ 字符串馈送=”http://feeds.feedburner.com/MetalMarketCommentary"; //http://globoesporte.globo.com/dynamo/futebol/times/vasco/rss2.xml //http://feeds.feedburner.com/GoldMoneyGoldResearch + //http://feeds.feedburner.com/GoldsilvercomNews + //http://feed43.com/7466558277232702.xml //http://feeds.feedburner.com/SilverGoldDaily //http://feeds.feedburner.com/MetalMarketCommentary //http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,VYXSD3OTBPHZ2UIRFX2 wdCLTYNyMNn&bclid=1644543007001&bctid=185418261001 RSSHandler rh=新的RSSHandler(); 列表文章=rh.getLatestArticles(提要); Log.e(“RSS错误”,“文章数”+articles.size()); 返回数据(文章); } 私有静态列表数据(列表项目){ 列表项=新建ArrayList(); 用于(第条:第条){ JSONObject当前=新的JSONObject(); 试一试{ buildJsonObject(文章,当前); }捕获(JSONException e){ e(“RSS错误”,“从RSS提要创建JSON对象时出错”); } 项目。添加(当前); } 退货项目; } 私有静态void buildJsonObject(Article-Article,JSONObject-current)抛出JSONException{ String title=article.getTitle(); 字符串描述=article.getDescription(); description=description.replaceFirst(“(?s)()(*?)”,“$1$3”); int start=description.indexOf(“.&”); description=description.substring(0,start); 字符串日期=article.getPubDate(); 字符串imgLink=article.getImgLink(); StringBuffer sb=新的StringBuffer(); sb.append(加粗打开)。append(标题)。append(加粗关闭); 某人追加(中断); 某人附加(描述); 某人追加(中断); sb.append(小开)。append(斜体开)。append(日期)。append(斜体闭)。append(小闭); current.put(“text”,Html.fromHtml(sb.toString()); 当前输入(“imageLink”,imgLink); } }
我正在解析的XML

<item>
           <title>Gold Market Recap Report</title>
           <link>http://feedproxy.google.com/~r/MetalMarketCommentary/~3/jGYtkXdSKWs/mid-session-gold_703.html</link>
           <description>&lt;img src="http://www.cmegroup.com/images/1x1trans.gif?destination=http://www.cmegroup.com/education/market-commentary/metals/2012/09/mid-session-gold_703.html" alt=""/&gt;For the week December gold forged a trading range of roughly $37 an ounce. With gold prices attimes seemingly on the rocks and poised for a downside washout it was a change of pace to see afresh upside breakout in the Friday morning trade....&lt;img src="http://feeds.feedburner.com/~r/MetalMarketCommentary/~4/jGYtkXdSKWs" height="1" width="1"/&gt;</description>
           <pubDate>Fri, 21 Sep 2012 19:50:37 GMT</pubDate>
           <guid isPermaLink="false">http://www.cmegroup.com/education/market-commentary/metals/2012/09/mid-session-gold_703.html?source=rss</guid>
           <dc:date>2012-09-21T19:50:37Z</dc:date>
           <feedburner:origLink>http://www.cmegroup.com/education/market-commentary/metals/2012/09/mid-session-gold_703.html?source=rss</feedburner:origLink>
      </item>

黄金市场回顾报告
http://feedproxy.google.com/~r/metalmarketcommentation/~3/jGYtkXdSKWs/mid-session-gold_703.html
img src=”http://www.cmegroup.com/images/1x1trans.gif?destination=http://www.cmegroup.com/education/market-Commentation/metals/2012/09/mid-session-gold_703.html“alt=”“/12月一周黄金的交易区间约为每盎司37美元。随着金价时节似乎处于震荡状态,并准备好了下跌的洗牌,周五早盘再次出现上涨突破是一种节奏的变化……img src=”http://feeds.feedburner.com/~r/metalmarketcommentation/~4/jGYtkXdSKWs“height=“1”width=“1”/
2012年9月21日星期五19:50:37 GMT
http://www.cmegroup.com/education/market-commentary/metals/2012/09/mid-session-gold_703.html?source=rss
2012-09-21T19:50:37Z
http://www.cmegroup.com/education/market-commentary/metals/2012/09/mid-session-gold_703.html?source=rss

这将删除<和>之间的任何内容,包括它们。

您不必使用正则表达式来执行此操作

创建一个方法,该方法将查找并删除以
开头、以
结尾的子字符串,并在循环中运行它

String mstring = "Beginning of String....&lt;img src="http://webaddress.com" height="1" width="1"/&gt;";
//  You'll need to escape the double quotes if you're explicitly setting the string

public boolean containsTags()
{
    boolean lt, gt;

    lt = mstring.contains("&lt;");
    gt = mstring.contains("&gt;");

    return (lt || gt);
}

public void removeTags()
{
    int indexof_lt, indexof_gt;

    indexof_lt = mstring.indexOf("&lt;");
    indexof_gt = mstring.indexOf("&gt;");

    String str_first, str_last;

    str_first = mstring.substring(0, indexof_lt);
    str_last = mstring.substring(indexof_gt + indexof_gt.length);

    mstring = str_first + str_last;
}

public void removeAllTags()
{
    while(containsTags)
    {
        removeTags();
    }
}

(1) 你说“没有成功”是什么意思?(2) 你为什么写
<
>
而不是
?(3) 您说您想删除
<
>
,但您的代码将
(<)(…)(>)
替换为
$1$3
,即
<
。为什么会这样?@ruakh我对regex不熟悉,从我读到的所有教程中,我的理解是,代码会删除“字符串开头”
major&;失败
感谢您的回复。它需要更具适应性,因为我正在将xml解析为字符串,然后附加html标记,这样以后就不能删除所有内容。我将发布我的课程。检查我的edit@B.Money给我一点时间,让你更能适应环境,就像一个魔咒。谢谢拉格哈。
String mstring = "Beginning of String....&lt;img src="http://webaddress.com" height="1" width="1"/&gt;";
//  You'll need to escape the double quotes if you're explicitly setting the string

public boolean containsTags()
{
    boolean lt, gt;

    lt = mstring.contains("&lt;");
    gt = mstring.contains("&gt;");

    return (lt || gt);
}

public void removeTags()
{
    int indexof_lt, indexof_gt;

    indexof_lt = mstring.indexOf("&lt;");
    indexof_gt = mstring.indexOf("&gt;");

    String str_first, str_last;

    str_first = mstring.substring(0, indexof_lt);
    str_last = mstring.substring(indexof_gt + indexof_gt.length);

    mstring = str_first + str_last;
}

public void removeAllTags()
{
    while(containsTags)
    {
        removeTags();
    }
}