Regular expression for a word.

I am making a string parsing problem in PHP. I want a regular expression for a word in a dictionary. The word can only contain AZ and az. I came up with this regex, but that doesn't seem to work. Can someone help.

$regexp = "/[Az]+/"; if(preg_match($regexp,$buffer)){ print $buffer . "<BR>"; } 

Adding rkt comment for eadibility

currently using this regex

 $regexp = "/[A-Za-z]+/"; 

but still many irrelevant words are printed, for example.

 a.new,#quickbar a.new{color:#ba0000} enwiki:resourceloader:filter:minify-css:5:f2a9127573a22335c2a9102b208c73e7 wgNamespaceNumber=0; wgAction="view"; wgPageName="Roger_Federer"; wgMainPageTitle="β€Œβ€‹Main Page"; wgWikimediaMobileUrl="http:\/\/en.m.wikipedia.org\/wiki"; document.writeln("\x3cdiv id=\"localNotice\"\x3e\x3cp\x3e\x3c/p\x3e\n\x3c/div\x3e"); 
+4
source share
5 answers

If you want to combine only the whole word ...

 /\b[AZ]+\b/i 
+5
source

Besides the problem that Limo Wang Kenobi noticed, your regular expression does not do what you think.

 /[A-Za-z]+/ 

All this checks that the letter, in any case, appears somewhere in the input. If your input looks like this:

 1111112222222333333333A333333444444555555567 

He will still fit.

What you are looking for is a regular expression:

 /^[A-Za-z]+$/ 

This will correspond to the beginning of the line, then one or more letters and, finally, the end of the line. Now there is no place for anything but letters!

+3
source

The regular expression should be /\b[A-Za-z]+\b/ , it was believed that for words to match in the dictionary, you probably want something more restrictive than this, because words usually don't have caps in any odd place.

0
source

your regular expression should be

 $regexp = "/[A-Za-z]+/"; 
0
source

You can check my answer on How do you separate a javascript string with spaces and punctuation? , which contains JavaScript, which can be used to break English text into an array of words and punctuation marks, where words can be hyphenated (clearly defined) or compound (will not).

0
source

All Articles