English 中文(简体)
普通表达方式与集团中相同
原标题:Regular expression that matches string equals to one in a group
  • 时间:2012-01-12 09:52:50
  •  标签:
  • regex

E.g. I want to match string with the same word at the end as at the begin, so that following strings match:

aaa dsfj gjroo gnfsdj riier aaa
sdf foiqjf skdfjqei  adf sdf sdjfei sdf
rew123 jefqeoi03945  jq984rjfa;p94  ajefoj384 rew123
最佳回答

这可以做以下工作:

/^(w+).*1$/

<>光度>

/           : regex delimiter
  ^         : start of string
    (       : start capture group 1
      w+   : one or more word character
          : word boundary
    )       : end of group 1
    .*      : any number of any char
          : word boundary
    1      : group 1
  $         : end of string
/           : regex delimiter
问题回答

M42的答案是,除原产物外,还不能用一字表示。 为了接受在一栏内使用的人:

/^(?:(w+).*1|w+)$/

同样,在非常大的护堤上,只配必要的部分可能大大加快。 这里我要谈谈我关于“javascript”的解决办法:

RegExp:

function areEdgeWordsTheSame(str) {
    var m = str.match(/^(w+)/);
    return (new RegExp(m[1]+ $ )).test(str);
}

String:

function areEdgeWordsTheSame(str) {
    var idx = str.indexOf(   );
    if (idx < 0) return true;
    return str.substr(0, idx) == str.substr(-idx);
}

我认为,这里的正确选择是不定期的。 为什么不将界线分成几个阵列,对第一个和最后一个项目进行比较:

c#:

string[] words = line.Split(   );
return words.Length >= 2 && words[0] == words[words.Length - 1];




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签