标签: matcher

当模式在行中找到时,Java正则表达式抛出异常,找不到匹配项

我正在试图弄清楚为什么正则表达式不匹配.任何帮助深表感谢.我将逐行浏览网页(工作正常),但我需要为每一行提取链接.应用程序将检查该行中是否有链接,但我需要实际提取URL.救命?

Pattern p = Pattern.compile("^.*href=\"([^\"]*)");
Matcher m = p.matcher(result);
String urlStr = m.group();
links.add(urlStr);
Run Code Online (Sandbox Code Playgroud)

我一直得到的错误信息是这样的:

Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)
Run Code Online (Sandbox Code Playgroud)

即使'result'中有一个链接引用(hxxp://www.yahoo.com).

links是一个ArrayList fyi.提前致谢!

java regex matcher

9
推荐指数
2
解决办法
1万
查看次数

在一个匹配器中匹配多个属性

我需要编写Matcher来检查多个属性.对于我使用的单一财产:

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

    Matcher<Class> matcherName = Matchers.<Class> hasProperty("propertyName",equalTo(expectedValue));
Run Code Online (Sandbox Code Playgroud)

我怎么想在一个匹配器中查看更多属性?

java hamcrest matcher mockito

9
推荐指数
2
解决办法
7835
查看次数

给定字符串中的混合重音和普通字符在搜索时无法在java中工作

String text = "Cámélan discovered ônte red ale?t \n Como se extingue la deuda";
Run Code Online (Sandbox Code Playgroud)

如果我给出输入Ca,它应该从给定的字符串Cá突出显示,但它不突出显示.

以下是我的尝试.

 Pattern mPattern; 
  String filterTerm; //this is the input which I give from input filter. Say for eg: Ca
   String regex = createFilterRegex(filterTerm);
        mPattern = Pattern.compile(regex);

 private String createFilterRegex(String filterTerm) {
        filterTerm = Normalizer.normalize(filterTerm, Normalizer.Form.NFD);
       filterTerm = filterTerm.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
        return filterTerm;
    }

public Pattern getPattern() {
        return mPattern;
    }
Run Code Online (Sandbox Code Playgroud)

在另一堂课上,

private SpannableStringBuilder createHighlightedString(String nodeText, int highlightColor) { //nodeText is the entire list displaying. 
        SpannableStringBuilder returnValue …
Run Code Online (Sandbox Code Playgroud)

java regex android matcher pattern-matching

9
推荐指数
1
解决办法
573
查看次数

如何避免使用 Matcher 在 SpaCy 中重复提取重叠模式?

我需要通过 python Spacy Matcher 从 2 个列表中提取项目组合。问题如下:让我们有2个列表:

colors=['red','bright red','black','brown','dark brown']
animals=['fox','bear','hare','squirrel','wolf']
Run Code Online (Sandbox Code Playgroud)

我通过以下代码匹配序列:

first_color=[]
last_color=[]
only_first_color=[]
for color in colors:
    if ' ' in color:
        first_color.append(color.split(' ')[0])
        last_color.append(color.split(' ')[1])
    else:
        only_first_color.append(color)
matcher = Matcher(nlp.vocab)

pattern1 = [{"TEXT": {"IN": only_first_color}},{"TEXT":{"IN": animals}}]
pattern2 = [{"TEXT": {"IN": first_color}},{"TEXT": {"IN": last_color}},{"TEXT":{"IN": animals}}]

matcher.add("ANIMALS", None, pattern1,pattern2)

doc = nlp('bright red fox met black wolf')

matches = matcher(doc)

for match_id, start, end in matches:
    string_id = nlp.vocab.strings[match_id]  # Get string representation
    span = doc[start:end]  # The matched …
Run Code Online (Sandbox Code Playgroud)

python nlp matcher spacy

9
推荐指数
2
解决办法
1274
查看次数

Java - 全名的正则表达式

如何验证正则表达式的全名?我只想要字母表(没有数字),只需要正则表达式的空格.这就是我到目前为止所做的.你能帮我修一下这个正则表达式吗?非常感谢你

public static boolean isFullname(String str) {
    boolean isValid = false;
    String expression = "^[a-zA-Z][ ]*$"; //I know this one is wrong for sure >,<
    CharSequence inputStr = str;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}
Run Code Online (Sandbox Code Playgroud)

java regex string validation matcher

8
推荐指数
2
解决办法
4万
查看次数

Java Scanner vs Matcher - 正则表达式,Matcher工作,Scanner没有

为什么第一个块可以工作但第二个块没有?

int numberOfDigits = 2;
Pattern p = Pattern.compile("[01]{"+numberOfDigits+"}");
Matcher m = p.matcher("101100101011010011111000");
while(m.find()){
    System.out.println(m.group());
}
Run Code Online (Sandbox Code Playgroud)

布洛克2

Scanner scannerSegment = new Scanner("101100101011010011111000");
   while(scannerSegment.hasNext(p)){
    String segment = scannerSegment.next(p);
        System.out.println(segment);

    }
Run Code Online (Sandbox Code Playgroud)

java regex matcher

8
推荐指数
1
解决办法
2732
查看次数

Mockito Matchers:匹配参数列表中的Class类型

我正在使用Java,Spring的RestTemplate和Mockito,使用Eclipse.我试图模拟Spring的rest模板,我模拟的方法的最后一个参数是Class类型.以下是该功能的签名:

public <T> ResponseEntity<T> exchange(URI url,
                                  HttpMethod method,
                                  HttpEntity<?> requestEntity,
                                  Class<T> responseType)
                       throws RestClientException
Run Code Online (Sandbox Code Playgroud)

我嘲笑这个方法的初步尝试如下:

//given restTemplate returns exception
when(restTemplate.exchange(isA(URI.class), eq(HttpMethod.POST), isA(HttpEntity.class), eq(Long.class))).thenThrow(new RestClientException(EXCEPTION_MESSAGE));
Run Code Online (Sandbox Code Playgroud)

但是,这行代码会从eclipse中产生以下错误:

The method exchange(URI, HttpMethod, HttpEntity<?>, Class<T>) in the type RestTemplate is not applicable for the arguments (URI, HttpMethod, HttpEntity, Class<Long>)
Run Code Online (Sandbox Code Playgroud)

Eclipse然后建议我使用'Class'强制转换投射最后一个参数,但是如果我将它转换为'Class'或其他类型,它似乎不起作用.

我一直在网上寻求帮助,但似乎对请求的参数是类类型的事实感到困惑.

到目前为止我看过的答案主要与通用集合有关.这里的任何帮助将不胜感激.

java generics matcher mockito

8
推荐指数
2
解决办法
1万
查看次数

如何使用isA-Matcher

我有一个提供Restriction-object 的特定方法(在哪里Restriction是一个接口).由于它的实现已经是测试,我只想测试我的方法是否实际上提供了一个对象RestrictionImpl.
我看到有一些我可以assertThat和它一起使用的匹配器,我想,匹配器isA是想要完成这项任务的东西.

简化我的代码看起来像这样:

public static Restriction getRestriction() {
    return new RestrictionImpl();
}
Run Code Online (Sandbox Code Playgroud)

我的测试看起来像那样;

@Test
public void getRestriction_returnsRestrictionImpl() {
    assertThat(getRestriction(), isA(RestrictionImpl.class));
}
Run Code Online (Sandbox Code Playgroud)

但是这不会编译.我所能做的就是测试,如果a RestrictionImplRestriction......但是没有必要这样做.

我误解了目的isA吗?它的意义是什么?

更新:
使用assertThat(getRestriction(), is(instanceOf(RestrictionImpl.class)))会工作,但我认为这isA是一个快捷方式.以我想要的方式
打电话assertThat需要它有签名assertThat(T, Matcher<? extends T>),但它的签名是assertThat(T, Matcher<? super T>)

java junit matcher junit4 assertthat

8
推荐指数
1
解决办法
5223
查看次数

如何使ExampleMatcher只匹配一个属性?

如何实现ExampleMatcher,从我的类中随机匹配一个属性并忽略其他属性?

假设我的课程是这样的:

Public Class Teacher() {
    String id;
    String name;
    String address;
    String phone;
    int area;
    ..other properties is here...
}
Run Code Online (Sandbox Code Playgroud)

如果我想按名称匹配:

Teacher TeacherExample = new Teacher("Peter");

ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase()
.withIgnorePaths("id", "address", "phone","area",...);   //no name 
Run Code Online (Sandbox Code Playgroud)

如果我想按地址匹配:

ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase()
.withIgnorePaths("id", "name", "phone","area",...); //no address
Run Code Online (Sandbox Code Playgroud)

所以我需要重复withIgnorePaths(..)如何避免这种情况?

java matcher query-by-example spring-data

8
推荐指数
1
解决办法
8367
查看次数

ValueError:nlp.add_pipe 现在采用已注册组件工厂的字符串名称,而不是可调用组件

以下链接展示了如何在实体跨越多个令牌的情况下添加自定义实体规则。执行此操作的代码如下:

import spacy
from spacy.pipeline import EntityRuler
nlp = spacy.load('en_core_web_sm', parse=True, tag=True, entity=True)

animal = ["cat", "dog", "artic fox"]
ruler = EntityRuler(nlp)
for a in animal:
    ruler.add_patterns([{"label": "animal", "pattern": a}])
nlp.add_pipe(ruler)


doc = nlp("There is no cat in the house and no artic fox in the basement")

with doc.retokenize() as retokenizer:
    for ent in doc.ents:
        retokenizer.merge(doc[ent.start:ent.end])


from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
pattern =[{'lower': 'no'},{'ENT_TYPE': {'REGEX': 'animal', 'OP': '+'}}]
matcher.add('negated animal', None, pattern)
matches = matcher(doc)


for …
Run Code Online (Sandbox Code Playgroud)

python matcher spacy

8
推荐指数
2
解决办法
2万
查看次数