我正在尝试制作某种聊天过滤器。我还想检查Button Start;
TextView textView;
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_line);
Start = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(this);
Start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://www.cricketlinepro.com/newapi.php?method=getRate",(String)null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("info");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject info = jsonArray.getJSONObject(i);
String Rate1 = info.getString("Rate1");
String Rate2 = info.getString("Rate2");
textView.append(Rate1 + " " + Rate2 + " \n");
}
} catch (
JSONException e)
{
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
}
});
}
}
或"wo rd"
甚至是"w ord"
。但是如果我输入“word wo rd”,它会输出"w.ord"
(应该是"**** **** ****"
)。此外,即使我禁用"**** ****"
检查,它也会忽略点..
这是我的代码:
dot
输入: //Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
NotAllowedWords.Add(new WordData("word", true, false));
var input = Console.ReadLine();
var splitted = input.Split(char.Parse(" "));
foreach (string s in splitted) {
var index = FindWord(input,s);
if (index != -1) {
input = input.Replace(s, "****");
}
}
Console.WriteLine(input);
}
private static int FindWord (string input, string word) {
var splitted = input.Split(char.Parse(" "));
bool next = false;
foreach (var w in splitted) {
foreach (var a in NotAllowedWords) {
if (a.CompareTo(word))
return input.IndexOf(word);
}
if (next) {
next = false;
if (w.Substring(0, word.Length - 1) == word.Substring(1))
return input.IndexOf(w);
}
if (w.Last() == word[0])
next = true;
}
return -1;
}
public static List<WordData> NotAllowedWords = new List<WordData>();
}
public class WordData {
protected WordData() {
IgnoreWhiteSpace = true;
}
public bool CompareTo(string word) {
var input = word;
if (this.NotAllowDots) {
input = input.Replace(".","");
}
if (this.IgnoreWhiteSpace) {
input = input.Trim();
}
if (input.Equals(input, StringComparison.CurrentCultureIgnoreCase) && !this.AllowedVersions.Contains(input))
return true;
return false;
}
public WordData(string word, bool ignoreWhiteSpace = true, bool notAllowDots = true, params string[] allowedVersions) : base() {
this.allowedVersions = allowedVersions.ToList();
this.IgnoreWhiteSpace = ignoreWhiteSpace;
this.NotAllowDots = notAllowDots;
this.Word = word;
}
public string Word { get; set;}
private List<string> allowedVersions = new List<string>();
public List<string> AllowedVersions {
get {
return allowedVersions;
}
}
public bool IgnoreWhiteSpace { get; set;}
public bool NotAllowDots {get; set;}
}
}
输出:word wo.rd wo rd
预期输出:**** **** **** ****
答案 0 :(得分:1)
以下是使用正则表达式进行操作的方法。您可以使用此在线工具来测试不同的模式http://regexstorm.net/tester
class Program
{
static void Main(string[] args)
{
var notAllowedWord= new NotAllowedWord("word");
var input = "replace word wo.rd also wor d and wor.d but not wordpress .";
var replacedText = notAllowedWord.ReplaceNotAllowedWord(input);
Console.WriteLine(replacedText);
//output
//replace **** ****also ****and ****but not wordpress .
Console.ReadLine();
}
public class NotAllowedWord
{
private readonly string _word;
private readonly Regex _wordRegEx;
private readonly string _replacement;
public NotAllowedWord(string word)
{
_word = word;
_replacement=new string('*',word.Length);
_wordRegEx = BuildRegExPattern();
}
private Regex BuildRegExPattern()
{
//TODO USE string builder
var pattern = "";
//create space and . match pattern
// example given "word" it will produce "w[ ]*\.*o[ ]*\.*r[ ]*\.*l[ ]*\.*d"
foreach (var c in _word)
{
pattern = pattern + c + @"[ ]*\.*";
}
var regex = $"({_word}[ ])" + //Match the word
$"|({pattern}[ ])" + //match word that contains space or .
$"|({pattern}$)" //match word that is at end of a sentence
;
//Console.WriteLine(regex);
return new Regex(regex);
}
public string ReplaceNotAllowedWord(string inputText)
{
return _wordRegEx.Replace(inputText, _replacement);
}
}
}