如何替换字符串中的多个子字符串实例(How to replace multiple instances of substring within a string)
我需要在同一个地方替换不同内容的大字符串的一些内容。
可以说我有一个字符串:
String str = "package blah.foo.bar;\n" + "import org.blah.blah;\n" + "import sel.this.that;\n" + "import sel.boo.foo;\n" + "...more text";
我想将单词
gen
插入以import sel.
开头的语句中import sel.
所以最终结果如下:import sel.gen.this.that;
我已经成功使用以下方法,但只有当我知道子字符串将在字符串中的位置时。 我的问题是,我不确定如何使以下代码动态:
private String modifyString(String str){ String hold = str.substring(0, str.indexOf(";")); hold = new StringBuilder(hold).insert(13, "gen.").toString(); str = str.replace("(?m)^package.*", hold); return str; }
上面的代码正确地替换了字符串
package blah.foo.bar;
与package blah.gen.foo.bar;
但这又一次只能替换字符串的开头部分。 我希望能够保存子字符串的所有实例,然后在这些子字符串中插入子字符串。
I need to replace some of the contents of a large string with different content in the same spot.
Lets say I have a string:
String str = "package blah.foo.bar;\n" + "import org.blah.blah;\n" + "import sel.this.that;\n" + "import sel.boo.foo;\n" + "...more text";
I want to insert the word
gen
into statements which start withimport sel.
So that the end result looks like:import sel.gen.this.that;
I have had success with the following method but only when I KNOW where that substring will be within the string. My issue is that I'm not sure how to make the following code dynamic:
private String modifyString(String str){ String hold = str.substring(0, str.indexOf(";")); hold = new StringBuilder(hold).insert(13, "gen.").toString(); str = str.replace("(?m)^package.*", hold); return str; }
The above code correctly replaces the strings
package blah.foo.bar;
withpackage blah.gen.foo.bar;
But again this only works with replacing the beginning portion of the string. I'm looking to be able to hold all instances of a substring and then insert a substring within those substrings.
原文:https://stackoverflow.com/questions/34344616
最满意答案
您必须同时使用GROUP BY区域和lang,如下所示:
SELECT COUNT(*) as `count`,`region`, `lang`, DATE(NOW()) as `week_ending` FROM mydata.table WHERE `date` > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND `date` < DATE(NOW()) GROUP BY `region`, `lang`, DATE(NOW());
You would have to GROUP BY both region and lang, like this:
SELECT COUNT(*) as `count`,`region`, `lang`, DATE(NOW()) as `week_ending` FROM mydata.table WHERE `date` > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND `date` < DATE(NOW()) GROUP BY `region`, `lang`, DATE(NOW());
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
MySQL过滤查询?(MySQL filter query?)[2024-01-11]
您必须同时使用GROUP BY区域和lang,如下所示: SELECT COUNT(*) as `count`,`region`, `lang`, DATE(NOW()) as `week_ending` FROM mydata.table WHERE `date` > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND `date` < DATE(NOW()) GROUP BY `region`, `lang`, DATE(NOW()); You would h ... -
查询中的问题是过滤后的别名的范围:从其他子查询中看不到它。 如果你真的想拥有它,你可以创建一个视图: create view filtered as SELECT * FROM push_message_info WHERE DATE(submit_date) > '2017-05-14'; SELECT p.pending, s.success, t.total FROM (SELECT count(*) AS pending FROM filtered WHERE served < total ...
-
在这些情况下,你通常可以替换if a then b else c与(a AND b) OR ((NOT a) AND c) ,如下所示: SELECT * FROM Orders WHERE (Ordertype='confidential' AND ReqUser='$UserID') OR (Ordertype<>'confidential' AND ODep) ; 或者,根据您的数据,这可能也有效: SELECT * FROM Orders WHERE ReqUser='$UserID ...
-
假设变量$genre和$model已经转义。 仅限演示代码,为清晰起见无安全性。 $sqlQuery = 'select * from games where 1=1'; if ( !empty($genre) ) $sqlQuery .= " and genre=$genre"; if ( !empty($model) ) $sqlQuery .= " and model=$model"; assuming variables $genre and $model already escaped. dem ...
-
你需要转义斜杠(\)。 在mysql中,斜杠(\)用于转义类似查询中的字符。 Select * from User where username NOT LIKE 'Abc\\\\%' You need to escape slash (\). In mysql slash (\) is used to escape characters in a like query. Select * from User where username NOT LIKE 'Abc\\\\%'
-
这应该做你需要的: $var1 = '1'; $var2 = '0'; $var3 = '1'; $where = array('1 = 1'); foreach (array('var1', 'var2', 'var3') as $var) { if ('1' == $$var) { $where[] = "$var = '1'"; } } // SELECT * FROM table WHERE 1 = 1 AND var1 = '1' AND var3 = '1' e ...
-
您可以采用不同的方法 首先请求clientId = 3的每个用户; select u.id from users u INNER JOIN users_clients uc ON u.userID = uc.userID WHERE uc.clientID =3; 然后您可以使用此请求来获取除clientId = 3之外的所有用户。 最终请求如下所示: select u.id from users u where u.id NOT IN (select u.id from users u INNER J ...
-
尝试这个: SELECT id, chapter_id, question, answer FROM (SELECT IF(@chapterId=@chapterId:=chapter_id, @id:=@id+1, @id:=0) queNo, id, chapter_id, question, answer FROM `questions`, (SELECT @chapterId:=0, @id:=0) AS A WHERE `chapter_id` IN (19, 20, ...
-
MySQL过滤查询(MySQL filters query)[2022-09-21]
如果你需要至少有一个过滤器的GID SELECT DISTINCT `gid` FROM `links` WHERE `filter_id` IN (1,2,3) 如果你需要GID,它有所有过滤器1,2,3 SELECT `gid` FROM `links` WHERE `filter_id` IN (1,2,3) GROUP BY `gid` HAVING COUNT( DISTINCT `filter_id`)=3 其中COUNT( DISTINCT filter_id )=3表示所有3个过滤 ...