Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][REGEXP] Dowolny wyraz ale bez spacji
daniel1302
post 14.01.2015, 08:55:20
Post #1





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

Ostrzeżenie: (0%)
-----


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.

Ten post edytował daniel1302 14.01.2015, 08:57:12
Go to the top of the page
+Quote Post
Pyton_000
post 14.01.2015, 09:08:37
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


([^\s])+
Go to the top of the page
+Quote Post
daniel1302
post 14.01.2015, 10:32:41
Post #3





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

Ostrzeżenie: (0%)
-----


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

Ten post edytował daniel1302 14.01.2015, 10:33:10
Go to the top of the page
+Quote Post
Pyton_000
post 14.01.2015, 10:36:46
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


[a-zA-Z]
Go to the top of the page
+Quote Post
daniel1302
post 14.01.2015, 10:52:20
Post #5





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

Ostrzeżenie: (0%)
-----


ale w wyrazie moge mieć dowlne znaki, %#@+-)( wszystkie, oprócz spacji
Go to the top of the page
+Quote Post
Pyton_000
post 14.01.2015, 10:56:06
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


no to [^\s] wystarczy
Go to the top of the page
+Quote Post
daniel1302
post 14.01.2015, 10:59:12
Post #7





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

Ostrzeżenie: (0%)
-----


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. }
Go to the top of the page
+Quote Post
Pyton_000
post 14.01.2015, 11:07:28
Post #8





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Pokaż tekst
Go to the top of the page
+Quote Post
daniel1302
post 14.01.2015, 11:30:50
Post #9





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

Ostrzeżenie: (0%)
-----


  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.
Go to the top of the page
+Quote Post
Pyton_000
post 14.01.2015, 11:33:46
Post #10





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


ok to możesz użyć takiego RegExp

"/\b\w{1,2}\b/"
zamiast tego co masz.
Go to the top of the page
+Quote Post
trueblue
post 14.01.2015, 11:58:44
Post #11





Grupa: Zarejestrowani
Postów: 6 799
Pomógł: 1827
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Ż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

Ten post edytował trueblue 14.01.2015, 11:59:38


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 4.05.2025 - 07:14