简体   繁体   中英

Regexp matching spaces in words

I am looking for a regular expression for replacing words written like "word", "overf low" to "word" and "overflow", respectively, all over the input string (including at the beginning and the end).

I'm using PHP & preg_replace. And need to detect those words before splitting string by space and further filtering. Any combinations for the time gives no due result.

Thanks in advance!

this one is hard because: what is a word? what is a part of a word?

What is different between in cr e di ble and it is a dog ?

For that to work with regex you need to set rules, for example:

  • How many whitespaces can be between words, and between word parts?
  • How long are the parts a word can be divided in?
  • Are ALL the words in the string divided in parts?

If you can't be sure of any of those rules then you can't write a regex that will solve the problem. Comparing the words to a dictionary to know if they are valid or not should prove more useful.

$ string = "w o r d", "o v e r f low"
$ echo 'word w o r d' | sed '/\([^ ][^ ]\)[ ]/s::\1\n:g ' | sed '/[ ]\([^ ][^ ]\)/s::\n\1:g' | sed ':r /\([^ ]\)[ ]/s::\1: ; tr' | sed 'H; ${x;/\n/s:: :g;sc[ ]cc;p};d'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM