Question

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';
Était-ce utile?

La solution

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

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top