开发者

Regular expression, finding a string contains but not starts with

I'm working on parsing a text file, running through each line, and I'm having trouble figuring out the Regex. Part of this parsing involves changing occurances such as:

É to é

I've tried the following regular expression, but it doesn't return anything:

/^(?!&开发者_StackOverflow社区)(É)/

Any suggestions?


So you want to match É only if it's not at the start of the line?

Use

/(?<=.)&Eacute;/

(assuming Ruby 1.9 because 1.8 doesn't support lookbehind)

Rationale: . matches any character except newline. So if the lookbehind assertion matches, we're not at the start of the line.


If you are using Ruby 1.9, you can use a lookbehind like this:

t.gsub! /(?<=.)&Eacute;/, '&eacute;'

In Ruby 1.8, you need to resort to something like this:

while t =~ /(.)&Eacute;/
    t.sub! /(.)&Eacute;/, "#{$1}&eacute;"
end

where t is your string to be modified.


If you want to match &Eacute; anywhere on the line, but not on the first position:

/^.+(&Eacute;)/

This matches e.g. "abc&Eacute;def", but not "&Eacute;"

UPDATE: fixed errors pointed out by Tim

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜