How to find a word that starts with a specific character
>>> import re
>>> text = "I was searching my source to make a big desk yesterday."
>>> re.findall(r'\bs\w+', text)
['searching', 'source']
For lowercase and uppercase s
use: r'\b[sS]\w+'
Regex to find words that start with a specific character
Search for:
- something that is not a word character then
- #
- some word characters
So try this:
/(?<!\w)#\w+/
Or in C# it would look like this:
string s = "Lorem ipsum #text Second lorem ipsum. How #are You. It's ok. Done. Something #else now.";
foreach (Match match in Regex.Matches(s, @"(?<!\w)#\w+"))
{
Console.WriteLine(match.Value);
}
Output:
#text
#are
#else
find all word start with specific character followed by digits in string
You could search for a word boundary, then the following letters mc
and some digits followed by another word boundary.
var string = "hi mc1001 hello mc1002 mc1003 mc1004 amc1005 mc mca mc1234a";
console.log(string.match(/\bmc\d+\b/g));
How can I find the word starting at a specific character position and not word postion in a string?
You can try.
Str="I love chocolate pudding"
pos=7
ans=Str[pos:].split()[0]
Output
"chocolate"
Str[pos:]
return 'chocolate pudding'
, Then I split them using split
which return ['chocolate','pudding']
and I extracted 1st-word using indexing.
If pos=8
the output would be 'hocolate'
.
How do I find words starting with a specific letter?
You can use word boundaries \b
, the following example shows how to match every word starting with t
var string ="hallo, this is a test john doe .Another tea house pole. Hey Tom."
result = string.match(/(\bt\S+\b)/ig);
//result = string.match(/(\st\S+)/ig); // alternative
document.write(result);
Find and replace all the words starting with a specific character
You can use a regular expression and FillAllPattern()
method to find the words that start with $ and return results in TextSelection collection.
Regex regex = new Regex(@"\$\w+\b");
TextSelection[] selections = document.FindAllPattern(regex);
To replace string that matches a specific regex with a new string, use Document.Replace(System.Text.RegularExpressions.Regex Pattern, string replace)
method.
Regex to find words that start or end with a particular letter
It you want the regex for this, then use:
regex = r'\b(#\w*[^#\W]|[^#\W]\w*#)\b'.replace('#', letter)
The replace
is done to avoid the repeated verbose +letter+
.
So the code looks like this then:
import re
def getWords(sentence, letter):
regex = r'\b(#\w*[^#\W]|[^#\W]\w*#)\b'.replace('#', letter)
return re.findall(regex, sentence, re.I)
s = "The TART program runs on Tuesdays and Thursdays, but it does not start until next week."
result = getWords(s, "t")
print(result)
Output:
['The', 'Tuesdays', 'Thursdays', 'but', 'it', 'not', 'start', 'next']
Explanation
I have used #
as a placeholder for the actual letter, and that will get replaced in the regular expression before it is actually used.
\b
: word break\w*
: 0 or more letters (or underscores)[^#\W]
: a letter that is not#
(the given letter)|
: logical OR. The left side matches words that start with the letter, but don't end with it, and the right side matches the opposite case.
Search the records with the specific word as starting word
You need to remove one of the wildcards, try it like this:
$sql="SELECT name FROM students WHERE name LIKE 'name%'";
The %
character means any character on this side.
So if you do %name
it means, anything that ends with name
and if you do name%
it means anything that starts with name. However, when you do %name%
it means, anything that contains the word name
Some examples:
SELECT name FROM students WHERE name LIKE 'name%'
Would match 'namemy' but not 'myname'
SELECT name FROM students WHERE name like '%name'
Would match 'myname' but not 'namemy'
SELECT name FROM students WHERE name like '%name%'
Would match both 'myname' AND 'namemy'
Related Topics
How to Plot Predicted Values VS the True Value
Regular Expression to Check Whitespace in the Beginning and End of a String
Simple Digit Recognition Ocr in Opencv-Python
How to Align Labels and Entry Boxes in a Gui Program
Matplotlib: Drawing Lines Between Points Ignoring Missing Data
How to Fix the 403:Insufficient Authentication Scopes Error from Google Analytics User Deletion API
Cmake on Linux Centos 7, How to Force the System to Use Cmake3
Python Pandas: Drop Rows of a Timeserie Based on Time Range
Convert CSV to Parquet File Using Python
Calling a Function of a Module by Using Its Name (A String)
How to Constantly Run Python Script in the Background on Windows
Adding a Data File in Pyinstaller Using the Onefile Option
How to Send Email to Multiple Recipients Using Python Smtplib
Print Floating Point Values Without Leading Zero
How to Stop Execution of Python Script in Visual Studio Code