Is there any function/procedure as ReplaceString but for whole words on Delphi? I just need to replace substring to the new one when it's a whole word. For example:
substring - > newstring
substring, -> newstring
substring123 -> substring
You can use the built-in regex library to do this. For example:
{$APPTYPE CONSOLE}
uses
System.RegularExpressions;
const
InputString = 'a Substring, substring123, some more text';
begin
Writeln(TRegEx.Replace(InputString, '\bsubstring\b', 'newstring', [roIgnoreCase]));
end.
Related
How do i check for the word "Hello" inside a string in an if statement but it should only detect if the word "Hello" is alone and not like "Helloo" or "HHello"
The easiest way to do such thing is to use regular expressions. By using regular expressions you can define a rule in order to validate a specific pattern.
Here is the rule for the pattern you required to be matched:
The word must contain the string "hello"
The string "hello" must be preceded by white-space, otherwise it must be the found at the beginning of the string to be matched.
The string "hello" must be followed by either a '.' or a white-space, Otherwise it must be found at the end of the string to be matched.
Here is a simple js code which implements the above rule:
let string = 'Hello, I am hello. Say me hello.';
const pattern = /(^|\s)hello(\s|.|$)/gi;
/*const pattern = /\bhello\b/ you can use this pattern, its easier*/
let matchResult = string.match(pattern);
console.log(matchResult);
In the above code I assumed that the pattern is not case sensitive. That is why I added case insensitive modifier ("i") after the pattern. I also added the global modifier ("g") to match all occurrence of the string "hello".
You can change the rule to whatever you want and update the regular expression to confirm to the new rule. For example you can allow for the string to be followed by "!". You can do that by simply adding "|!" after "$".
If you are new to regular expressions I suggest you to visit W3Schools reference:
https://www.w3schools.com/jsref/jsref_obj_regexp.asp
One way to achieve this is by first replacing all the non alphabetic characters from string like hello, how are you #NatiG's answer will fail at this point, because the word hello is present with a leading , but no empty space. once all the special characters are removed you can simply split the string to array of words and filter 'hello' from there.
let text = "hello how are you doing today? Helloo HHello";
// Remove all non alphabetical charachters
text = text.replace(/[^a-zA-Z0-9 ]/g, '')
// Break the text string to words
const myArray = text.split(" ");
const found = myArray.filter((word) => word.toLowerCase() == 'hello')
// to check the array of found ```hellos```
console.log(found)
//Get the found status
if(found.length > 0) {
console.log('Found')
}
Result
['hello']
Found
This question already has answers here:
Extracting digits from a cell with varying char length
(4 answers)
Closed 2 years ago.
I need to be able to remove all alphabetical characters from a string, leaving just the numbers behind.
I don't need to worry about any other characters like ,.?# and so on, just the letters of the alphabet a-z, regardless of case.
The closest I could get to a solution was the exact opposite, the below VBA is able to remove the numbers from a string.
Function removenumbers(ByVal input1 As String) As String
Dim x
Dim tmp As String
tmp = input1
For x = a To Z
tmp = Replace(tmp, x, "")
Next
removenumbers = tmp
End Function
Is there any modification I can make to remove the letters rather than numbers to the above, or am I going at this completely wrong.
The letters could fall anywhere in the string, and there is no pattern to the strings.
Failing this I will use CTRL + H to remove all letters one by one, but may need to repeat this again each week so UDF would be much quicker.
I'm using Office 365 on Excel 16
Option Explicit
dim mystring as String
dim regex as new RegExp
Private Function rgclean(ByVal mystring As String) As String
'function that find and replace string if contains regex pattern
'returns str
With regex
.Global = True ' return all matches found in string
.Pattern = "[A-Z]" ' add [A-Za-z] if you want lower case as well the regex pattern will pick all letters from A-Z and
End With
rgclean = regex.Replace(mystring, "") '.. and replaces everything else with ""
End Function
Try using regular expression.
Make sure you enable regular expression on: Tools > References > checkbox: "Microsoft VBScript Regular Expressions 5.5"
The function will remove anything from [A-Z], if you want to include lower case add [A-Za-z] into the regex.pattern values. ( .Pattern = "[A-Za-z]")
You just pass the string into the function, and the function will use regular expression to remove any words from in a string
Thanks
Is there a function in Lua that returns a string inserted into another one on given position?
For example string.insert(str1, str2, pos).
Using it: string.insert('Hello World!', 'My ', 6) becomesHello My World! and so on.
There is no such function in the standard Lua library. But it's easy to write one:
function string.insert(str1, str2, pos)
return str1:sub(1,pos)..str2..str1:sub(pos+1)
end
Note how it automatically handles negative positions (*), which count from the end of the string, as most other string functions do.
(*) it needs a small change to make pos=-1 work.
The other answers for similar question works if the string is str1 = 'MynameisJohn' within single quotes. For example, str1(1:2) gives 'My'.
But if the string is str1 = "MynameisJohn" with double quotes, the above usage str1(1:2) does not work and gives an out of bounds error. The size of str1 in this case is just a 1 by 1 matrix.
In the second case, how do I split the string to get the words in it, assuming there are no whitespaces (hence delimiters can't be used). We can assume the lenghts of my split are constant.
EDIT
I think I found the answer myself. str2 = char(str1) converts the string array str1 to a character array and then similar constructs str2(1:2) works.
Conversion to char and then indexing works as you have posted. If you would like the result to stay as a string another way to extract substring is to use extract functions. For example,
str1 = string('MynameisJohn');
substr = extractBefore(str1,3)
substr =
string
"My"
In this case substr is still a string type. Doc for extractBefore is at https://www.mathworks.com/help/matlab/ref/extractbefore.html
Is there any function/procedure as ReplaceString but for whole words on Delphi? I just need to replace substring to the new one when it's a whole word. For example:
substring - > newstring
substring, -> newstring
substring123 -> substring
You can use the built-in regex library to do this. For example:
{$APPTYPE CONSOLE}
uses
System.RegularExpressions;
const
InputString = 'a Substring, substring123, some more text';
begin
Writeln(TRegEx.Replace(InputString, '\bsubstring\b', 'newstring', [roIgnoreCase]));
end.