简体   繁体   English

正则表达式在Java中查找带连字符的文本

[英]regex to find hyphenated text in java

I need find out only hyphenated text like this, i made some code for this its working but it has some problem. 我只需要找到像这样的带连字符的文本,就可以正常工作了,但是做了一些代码。

Eg:-(PRX)-is a non-steroidal surface-to-volume-ratio-due-to-the characterized-by a poorly water solubility.(Key)-words " Only non-steroidal is valid only." 例如:-( PRX)-是由于其非甾体的表面积与体积之比,以水溶性差为特征。(关键词)-“只有非甾体才有效。”

  1. non-steroidal--->valid (only this type is valid rest all should be neglected 非类固醇--->有效 (仅此类型有效,其余都应忽略
  2. surface-to-volume-ratio-due-to-the---> not valid 面积与体积之比->无效
  3. (Key)-words:----> not valid (关键字)-单词:---->无效

only one hyphen is allowed. 只能使用一个连字符。 if there are 2 hyphen in a word then it should be neglected. 如果一个单词中有两个连字符,则应将其忽略。

my regex is--: 我的正则表达式是-:

(((\b|,|'|.!|)(([a-zA-Z])+){0}-{1}(([a-zA-Z]+))(\b|,|'|.!)))

as in this fig: red circle are showing that must be neglected. 如图所示:红色圆圈表示必须忽略。 在此处输入图片说明

If you want to get words with only one hyphen you can use this regex: 如果您只想使用一个连字符来获取单词,则可以使用此正则表达式:

(?<!-)\b(\w+\-\w+)\b(?!-)

Working demo 工作演示

在此处输入图片说明

But if you want to get hyphened words (with multiple hyphens) you can use: 但是,如果要获取带连字符的单词(带有多个连字符) ,可以使用:

((?:\w+\-)+\w+)

Working demo 工作演示

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM