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
/(?<=.)É/
(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! /(?<=.)É/, 'é'
In Ruby 1.8, you need to resort to something like this:
while t =~ /(.)É/
t.sub! /(.)É/, "#{$1}é"
end
where t is your string to be modified.
If you want to match É
anywhere on the line, but not on the first position:
/^.+(É)/
This matches e.g. "abcÉdef"
, but not "É"
UPDATE: fixed errors pointed out by Tim
精彩评论