I need a regex which matches code fragments like:

'Lo'.'re'.'m'.' Ipsum'.' dol'.'or'

I've no idea how to even start. How would I do that if I know that the final string is Lorem ipsum dolor but I don't know the positions or the amount of '.'?

I'm trying to find this string in some source files with grep over the command line. In the code it can for example look like.

$random = 'Lo'.'re'.'m'.' Ipsum'.' dol'.'or';
有帮助吗?

解决方案

You can use tr:

s="'Lo'.'re'.'m'.' Ipsum'.' dol'.'or'"
echo "$s" | tr -d "[.']"
Lorem Ipsum dolor

To validate with grep:

echo "$s" | tr -d "[.']" | grep -q 'Lorem Ipsum dolor'
echo $?
0

其他提示

Replace all not letter/space characters with nothing (delete them):

You haven't said what language you're using, so all I can do is provide the search and replace terms:

Search: "['.]"
Replace: "" (nothing)

Well, you can do some crazy stuff like making optional '.' after each letter.

L(?:'\.')?o(?:'\.')?r(?:'\.')?e(?:'\.')?m(?:'\.')? (?:'\.')?I(?:'\.')?p(?:'\.')?s(?:'\.')?u(?:'\.')?m(?:'\.')? (?:'\.')?d(?:'\.')?o(?:'\.')?l(?:'\.')?o(?:'\.')?r
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top