我对正则表达式(正则表达式)有一个小问题。
我想删除字符串中每个单词末尾的任何“T” 这是我用来显示以“T”结尾的所有单词的代码。
public static void main (String []args){
String name = "PHYLAURHEIMSMET hello tttttyyuolktttb fedqs jhgjt";
p = Pattern.compile("([a-z0-9]+)?[t]");
m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
}
感谢您的帮助。
答案 0 :(得分:0)
我尝试删除每个单词末尾的任何“T”
_registeredItems
这将匹配每个单词末尾出现的所有string.replaceAll("T(?!\\S)", "");
。
或强>
T
只有在空格或行锚点结束时才会匹配所有string.replaceAll("T(?=\\s|$)", "");
。
进行不区分大小写的替换。
t
答案 1 :(得分:0)
我会使用S = malloc(10*sizeof(int));
if (S == NULL)
{
//Error handling supposed to be done if no memory is allocated
}
字边界来测试字母是否在单词的末尾,您可以在this regex101上看到将#include <stdio.h>
#include <stdlib.h>
typedef int data_t;
int top;
void push(data_t,int*);
int pop(int*);
int
main(int argc, char **argv)
{
int *S = NULL, i;
top = -1;
S = malloc(10*sizeof(int));
if(S == NULL)
{
printf("Memory Allocation failed");
//other error handling implementation if any
}
push(1,S);
push(2,S);
push(3,S);
for(i = 0; i <= top; i++){
printf("%d\n",S[i]);
}
printf("\n Popping:\n");
pop(S);
for(i = 0; i <= top; i++){
printf("%d\n",S[i]);
}
return 0;
}
void
push(data_t n,int *S )
{
//Check if Stack is full
if (top == 10)
{
printf("Stack Full");
//other implementation
}
else
{
++top;
S[top] = n;
}
}
int
pop(int *S)
{
//Check if Stack is empty
if (top == -1)
{
printf("Stack Empty");
//other implementation
}
else
{
return S[top--];
}
}
替换为空字符串的结果
我不确定您是否要仅删除大写字母T,在这种情况下,请将\b
一直移到右侧。< / p>
在Java中将是:
t\b
答案 2 :(得分:0)
这只是建议为什么不使用这样的简单String类内置函数:
String name = "PHYLAURHEIMSMET hello tttttyyuolktttb fedqs jhgjt";
if ('t' == name.charAt(name.length() - 1) || 'T' == name.charAt(name.length() - 1)) {
System.out.println("contains last char 't' or 'T'");
}