Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][REGEXP] Dowolny wyraz ale bez spacji
Forum PHP.pl > Forum > Przedszkole
daniel1302
Witam potrzebuję wyciągnąć z tekstu wyraz składający się z dowolnego znaku ale nie z spacji.

I Nie wiem jak zanegować biały znak \s.

Próbowałem tak:
  1. (.*[^\s])+
ale nie potrafie sobie poradzić.
Bez tej negacji to zwraca mi cały tekst.
Czy jest możliwośc zanegowania spacji bo mogę dać spacje z przodu i z tyłu
  1. (\040(.*)+\040)+

Ale chce się dowiedzieć jak rozwiązać taki problem na przyszłość
Pozdrawiam.
Pyton_000
([^\s])+
daniel1302
Chcę wyszukać wszystkie słowa Jedno i dwuliterowe w tekscie i robię to tak:
  1. $text = 'A It seems to me like using a REGEX in this case would just be overkill. Why not just just strpos to find the space character. Also, theres nothing special about the space character in regular expressions, you should be able to search for it the same as you would search for any other character. That is, unless you disabled pattern whitespace, which would hardly be necessary in this case.';
  2. preg_match_all('/\s+([a-z][^\s]){1,2}\s+/', $text, $matches);


A funkcja zwraca głupoty:

  1. array(2) {
  2. [0]=>
  3. array(14) {
  4. [0]=>
  5. string(4) " to "
  6. [1]=>
  7. string(6) " like "
  8. [2]=>
  9. string(4) " in "
  10. [3]=>
  11. string(6) " case "
  12. [4]=>
  13. string(6) " just "
  14. [5]=>
  15. string(6) " just "
  16. [6]=>
  17. string(4) " to "
  18. [7]=>
  19. string(4) " in "
  20. [8]=>
  21. string(4) " be "
  22. [9]=>
  23. string(4) " to "
  24. [10]=>
  25. string(4) " it "
  26. [11]=>
  27. string(6) " same "
  28. [12]=>
  29. string(4) " be "
  30. [13]=>
  31. string(4) " in "
  32. }
  33. [1]=>
  34. array(14) {
  35. [0]=>
  36. string(2) "to"
  37. [1]=>
  38. string(2) "ke"
  39. [2]=>
  40. string(2) "in"
  41. [3]=>
  42. string(2) "se"
  43. [4]=>
  44. string(2) "st"
  45. [5]=>
  46. string(2) "st"
  47. [6]=>
  48. string(2) "to"
  49. [7]=>
  50. string(2) "in"
  51. [8]=>
  52. string(2) "be"
  53. [9]=>
  54. string(2) "to"
  55. [10]=>
  56. string(2) "it"
  57. [11]=>
  58. string(2) "me"
  59. [12]=>
  60. string(2) "be"
  61. [13]=>
  62. string(2) "in"
  63. }
  64. }


Czemu zwraca końcówki od wyrazów, a nie zwraca np pierwszego wyrazu jakim jestA
Pyton_000
[a-zA-Z]
daniel1302
ale w wyrazie moge mieć dowlne znaki, %#@+-)( wszystkie, oprócz spacji
Pyton_000
no to [^\s] wystarczy
daniel1302
Dzięki, jest lepiej, ale dalej zwraca mi nie wszystko co potrzebuje:

  1. preg_match_all('/\s+([^\s]{1,2})\s+/i', $text, $matches);


Czemu nie zwraca mi pierwszego wyrazu(A) i wyrazu me. One sa w zakresie i powinny spełnić założenia
  1. array(2) {
  2. [0]=>
  3. array(13) {
  4. [0]=>
  5. string(4) " It "
  6. [1]=>
  7. string(4) " to "
  8. [2]=>
  9. string(3) " a "
  10. [3]=>
  11. string(4) " in "
  12. [4]=>
  13. string(4) " be "
  14. [5]=>
  15. string(4) " to "
  16. [6]=>
  17. string(4) " in "
  18. [7]=>
  19. string(4) " be "
  20. [8]=>
  21. string(4) " to "
  22. [9]=>
  23. string(4) " it "
  24. [10]=>
  25. string(4) " as "
  26. [11]=>
  27. string(4) " be "
  28. [12]=>
  29. string(4) " in "
  30. }
  31. [1]=>
  32. array(13) {
  33. [0]=>
  34. string(2) "It"
  35. [1]=>
  36. string(2) "to"
  37. [2]=>
  38. string(1) "a"
  39. [3]=>
  40. string(2) "in"
  41. [4]=>
  42. string(2) "be"
  43. [5]=>
  44. string(2) "to"
  45. [6]=>
  46. string(2) "in"
  47. [7]=>
  48. string(2) "be"
  49. [8]=>
  50. string(2) "to"
  51. [9]=>
  52. string(2) "it"
  53. [10]=>
  54. string(2) "as"
  55. [11]=>
  56. string(2) "be"
  57. [12]=>
  58. string(2) "in"
  59. }
  60. }
Pyton_000
Pokaż tekst
daniel1302
  1. $text = 'A It seems to me like using a REGEX in this case would just be overkill. Why not just just strpos to find the space character. Also, theres nothing special about the space character in regular expressions, you should be able to search for it the same as you would search for any other character. That is, unless you disabled pattern whitespace, which would hardly be necessary in this case.';


Tekst mam cały czas taki sam
Cytat
A It seems to me like using a REGEX in this case would just be overkill. Why not just just strpos to find the space character. Also, theres nothing special about the space character in regular expressions, you should be able to search for it the same as you would search for any other character. That is, unless you disabled pattern whitespace, which would hardly be necessary in this case.
Pyton_000
ok to możesz użyć takiego RegExp

"/\b\w{1,2}\b/"
zamiast tego co masz.
trueblue
Żeby się nie brało pod uwagę fragmentów typu:
co-worker
0.1
adobe.com/go/flex
http: //RegExr.com/v1?2rjl6

\b(?<![\w-\/\.\[\]\?])(\S){1,2}(?![\w-\/\.\[\]\?])\b
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.