Regex to find middle initial after first name - c#-4.0
Language C#. In the 'Leave' event of a textbox, I'm using Regex to try to find a single Middle Initial following a FirstName and a Space. If a match is found, I then TitleCase the beginning character of the FirstName and MI and add a period "." after the middle initial. Otherwise, I just TitleCase the FirstName.
I show a section of my code below:
private void FirstName_Leave(object sender, EventArgs e)
{
//Regex looks for a single letter following first name, i.e.
//middle initial, and adds a '.' period after the letter.
Match match = Regex.Match(FirstName.Text, #"\w+\s[a-zA-Z]");
if (match.Success)
{
FirstName.Text = ReturnTitleCase(FirstName.Text + ".");
}
else
{
FirstName.Text = ReturnTitleCase(FirstName.Text);
}
}
This works fine when the end-user enters just a single character after the FirstName, i.e., Mike E.
However, if the user enters a fully spelled out middle name such as Mike Edward, then a period is entered after the middle name like 'Mike Edward.'
I've also tried other Regex syntax such as the following, from a StackOverFlow site shown below.looking for one or more characters in the FirstName, then one or more Space characters, followed by just a Single word character. However, this again adds a period "." after a spelled out middle name.
regex middle name initial with or without it there
Match match = Regex.Match(FirstName.Text, #"\w+[a-zA-Z]\s+\w?[a-zA-Z]");
Could someone suggest a Regex syntax that would check for Only a single character after the FirstName, but would ignore anything longer than just a single character middle initial?
Related
Find and replace text and wrap in "href"
I am trying to find specific word in a div (id="Test") that starts with "a04" (no case). I can find and replace the words found. But I am unable to correctly use the word found in a "href" link. I am trying the following working code that correctly identifies my search criteria. My current code is working as expected but I would like help as i do not know how to used the found work as the url id? var test = document.getElementById("test").innerHTML function replacetxt(){ var str_rep = document.getElementById("test").innerHTML.replace(/a04(\w)+/g,'TEST'); var temp = str_rep; //alert(temp); document.getElementById("test").innerHTML = temp; } I would like to wrap the found word in an href but i do not know how to use the found word as the url id (url.com?id=found word). Can someone help point out how to reference the found work please? Thanks
If you want to use your pattern with the capturing group, you could move the quantifier + inside the group or else you would only get the value of the last iteration. \ba04(\w+) \b word boundary to prevent the match being part of a longer word a04 Match literally (\w+) Capture group 1, match 1+ times a word character Regex demo Then you could use the first capturing group in the replacement by referring to it with $1 If the string is a04word, you would capture word in group 1. Your code might look like: function replacetxt(){ var elm = document.getElementById("test"); if (elm) { elm.innerHTML = elm.innerHTML.replace(/\ba04(\w+)/g,'TEST'); } } replacetxt(); <div id="test">This is text a04word more text here</div> Note that you don't have to create extra variables like var temp = str_rep;
Copy portion of text in a file and paste somewhere else on the same line within the file
I have a text file (.cs file) with some information I want to copy from within the file to some place else on the same line within the file. For instance, I have something like: Random text here { Name = "Important - info", UniqueId = "1110100", More random text}; Random text here { Name = "More Important (info)", UniqueId = "1110101", More random text}; Random text here { Name = "Other Important info", UniqueId = "1110102", More random text}; . . . Random text here { Name = "Other Important info 4/11", UniqueId = "2000110", More random text}; and I want the numbers after UniqueId to be copied and placed at the end of the name on the same line like so Random text here { Name = "Important - info 1110100", UniqueId = "1110100", More random text}; Random text here { Name = "More Important (info) 1110101", UniqueId = "1110101", More random text}; Random text here { Name = "Other Important info 1110102", UniqueId = "1110102", More random text}; . . . Random text here { Name = "Other Important info 4/11 2000110", UniqueId = "2000110", More random text}; As long as there is a space after the quoted name and then the UniqueId. The name can be anything, the UniqueId is unique for each name and each line. Is this task possible? I'll try any Windows program.
You can accomplish what you are asking by using any text editor that supports Regex. In this example, I will be using Notepad++. First I will describe what to do, then after I will explain what the Regex does. Example Open the file with Notepad++. Press Ctrl+F to bring up the Search and Replace window. Make sure to check the box named Wrap around Select Regular expression under Search Mode Under Find What: insert Name = "(.*)", UniqueId = "(\d+)" Under Replace with: insert Name = "$1 $2", UniqueId = "$2" Press Replace all once. Understanding the regex () these characters represent a group that you want to capture. $1 represent the first group that you marked with () $2 same thing but it takes the second group. \d matches any digit. + after a digit means, match one or more digits. . matches any character. * after the dot means match zero or more characters. In the search example Name = "(.*)", UniqueId = "(\d+)" we have two groups. Matching the content in between the quotes of Name and UniqueId. In the replace example Name = "$1 $2", UniqueId = "$2", we are using these groups to replace what we match with something new. The new is, in this case, the contents of group $1 and group $2.
How to replace unwanted characters
I have some hotels that contains characters which are not valie for when i want to insert these hotel names as a file name as file naming doesn't allow /, * or ? and want to know what this error means. text?text text?text text**text** text*text (text) text *text* text? I am trying to use an if else statement so that if a hotel name contains any of these characters, then replace them with -. However I am receiving and error stating a dangling ?. I just want to check if I am using the replace correctly for these characters. def hotelNameTrim = hotelName.toString() if (hotelNameTrim.contains("/")) { hotelNameTrim.replaceAll("/", "-") } else if (hotelNameTrim.contains("*")) { hotelNameTrim.replaceAll("*", "-") } else if (hotelNameTrim.contains("?")) { hotelNameTrim.replaceAll("?", "-") }
replaceAll accepts a regex as a search pattern. * and ? are special characters in regex and need to be escaped with a back slash. Which itself needs to be escaped in a Java string :) Try this: hotelNameTrim.replaceAll("[\\*\\?/]","-") That will replace all you characters with a dash.
Find index of a specific character in a string then parse the string
I have strings which looks like this [NAME LASTNAME/NAME.LAST#emailaddress/123456678]. What I want to do is parse strings which have the same format as shown above so I only get NAME LASTNAME. My psuedo idea is find the index of the first instance of /, then strip from index 1 to that index of / we found. I want this as a VBScript.
Your way should work. You can also Split() your string on / and just grab the first element of the resulting array: Const SOME_STRING = "John Doe/John.Doe#example.com/12345678" WScript.Echo Split(SOME_STRING, "/")(0) Output: John Doe Edit, with respect to comments. If your string contains the [, you can still Split(). Just use Mid() to grab the first element starting at character position 2: Const SOME_STRING = "[John Doe/John.Doe#example.com/12345678]" WScript.Echo Mid(Split(SOME_STRING, "/")(0), 2)
Your idea is good here, you should also need to grab index for "[".This will make script robust and flexible here.Below code will always return strings placed between first occurrence of "[" and "/". var = "[John Doe/John.Doe#example.com/12345678]" WScript.Echo Mid(var, (InStr(var,"[")+1),InStr(var,"/")-InStr(var,"[")-1)
Lua: Search a specific string
Hi all tried all the string pattrens and library arguments but still stuck. i want to get the name of the director from the following string i have tried the string.matcH but it matches the from the first character it finD from the string the string is... fixstrdirector = {id:39254,cast:[{id:15250,name:Hope Davis,character:Aunt Debra,order:5,cast_id:10,profile_path:/aIHF11Ss8P0A8JUfiWf8OHPVhOs.jpg},{id:53650,name:Anthony Mackie,character:Finn,order:3,cast_id:11,profile_path:/5VGGJ0Co8SC94iiedWb2o3C36T.jpg},{id:19034,name:Evangeline Lilly,character:Bailey Tallet,order:2,cast_id:12,profile_path:/oAOpJKgKEdW49jXrjvUcPcEQJb3.jpg},{id:6968,name:Hugh Jackman,character:Charlie Kenton,order:0,cast_id:13,profile_path:/wnl7esRbP3paALKn4bCr0k8qaFu.jpg},{id:79072,name:Kevin Durand,character:Ricky,order:4,cast_id:14,profile_path:/c95tTUjx5T0D0ROqTcINojpH6nB.jpg},{id:234479,name:Dakota Goyo,character:Max Kenton,order:1,cast_id:15,profile_path:/7PU6n4fhDuFwuwcYVyRNVEZE7ct.jpg},{id:8986,name:James Rebhorn,character:Marvin,order:6,cast_id:16,profile_path:/ezETMv0YM0Rg6YhKpu4vHuIY37D.jpg},{id:930729,name:Marco Ruggeri,character:Cliff,order:7,cast_id:17,profile_path:/1Ox63ukTd2yfOf1LVJOMXwmeQjO.jpg},{id:19860,name:Karl Yune,character:Tak Mashido,order:8,cast_id:18,profile_path:/qK315vPObCNdywdRN66971FtFez.jpg},{id:111206,name:Olga Fonda,character:Farra Lemkova,order:9,cast_id:19,profile_path:/j1qabOHf3Pf82f1lFpUmdF5XvSp.jpg},{id:53176,name:John Gatins,character:Kingpin,order:10,cast_id:41,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:1126350,name:Sophie Levy,character:Big Sister,order:11,cast_id:42,profile_path:null},{id:1126351,name:Tess Levy,character:Little Sister,order:12,cast_id:43,profile_path:null},{id:1126352,name:Charlie Levy,character:Littlest Sister,order:13,cast_id:44,profile_path:null},{id:187983,name:Gregory Sims,character:Bill Panner,order:14,cast_id:45,profile_path:null}],crew:[{id:58726,name:Leslie Bohem,department:Writing,job:Screenplay,profile_path:null},{id:53176,name:John Gatins,department:Writing,job:Screenplay,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:17825,name:Shawn Levy,department:Directing,job:Director,profile_path:/7f2f8EXdlWsPYN0HPGcIlG21xU.jpg},{id:12415,name:Richard Matheson,department:Writing,job:Story,profile_path:null},{id:57113,name:Dan Gilroy,department:Writing,job:Story,profile_path:null},{id:25210,name:Jeremy Leven,department:Writing,job:Story,profile_path:null},{id:17825,name:Shawn Levy,department:Production,job:Producer,profile_path:/7f2f8EXdlWsPYN0HPGcIlG21xU.jpg},{id:34970,name:Susan Montford,department:Production,job:Producer,profile_path:/1XJt51Y9ciPhkHrAYE0j6Jsmgji.jpg},{id:3183,name:Don Murphy,department:Production,job:Producer,profile_path:null},{id:34967,name:Rick Benattar,department:Production,job:Producer,profile_path:null},{id:1126348,name:Eric Hedayat,department:Production,job:Producer,profile_path:null},{id:186721,name:Ron Ames,department:Production,job:Producer,profile_path:null},{id:10956,name:Josh McLaglen,department:Production,job:Executive Producer,profile_path:null},{id:57634,name:Mary McLaglen,department:Production,job:Executive Producer,profile_path:null},{id:23779,name:Jack Rapke,department:Production,job:Executive Producer,profile_path:null},{id:488,name:Steven Spielberg,department:Production,job:Executive Producer,profile_path:/cuIYdFbEe89PHpoiOS9tmo84ED2.jpg},{id:30,name:Steve Starkey,department:Production,job:Executive Producer,profile_path:null},{id:24,name:Robert Zemeckis,department:Production,job:Executive Producer,profile_path:/isCuZ9PWIOyXzdf3ihodXzjIumL.jpg},{id:531,name:Danny Elfman,department:Sound,job:Original Music Composer,profile_path:/pWacZpYPos8io22nEiim7d3wp2j.jpg},{id:18265,name:Mauro Fiore,department:Crew,job:Cinematography,profile_path:null},{id:54271,name:Dean Zimmerman,department:Editing,job:Editor,profile_path:null},{id:25365,name:Richard Hicks,department:Production,job:Casting,profile_path:null},{id:5490,name:David Rubin,department:Production,job:Casting,profile_path:null},{id:52088,name:Tom Meyer,department:Art,job:Production Design,profile_path:null}]} i have tried string.match(fixstrdirector,"name:(.+),department:Directing") but it gives me the from the first occurace it find the name to the end of thr string output: Hope Davis,character:Aunt Debra,order:5,cast_id:10,profile_path:/aIHF11Ss8P0A8JUfiWf8OHPVhOs.jpg},{id:53650,name:Anthony Mackie,character:Finn,order:3,cast_id:11,profile_path:/5VGGJ0Co8SC94iiedWb2o3C36T.jpg},{id:19034,name:Evangeline Lilly,character:Bailey Tallet,order:2,cast_id:12,profile_path:/oAOpJKgKEdW49jXrjvUcPcEQJb3.jpg},{id:6968,name:Hugh Jackman,character:Charlie Kenton,order:0,cast_id:13,profile_path:/wnl7esRbP3paALKn4bCr0k8qaFu.jpg},{id:79072,name:Kevin Durand,character:Ricky,order:4,cast_id:14,profile_path:/c95tTUjx5T0D0ROqTcINojpH6nB.jpg},{id:234479,name:Dakota Goyo,character:Max Kenton,order:1,cast_id:15,profile_path:/7PU6n4fhDuFwuwcYVyRNVEZE7ct.jpg},{id:8986,name:James Rebhorn,character:Marvin,order:6,cast_id:16,profile_path:/ezETMv0YM0Rg6YhKpu4vHuIY37D.jpg},{id:930729,name:Marco Ruggeri,character:Cliff,order:7,cast_id:17,profile_path:/1Ox63ukTd2yfOf1LVJOMXwmeQjO.jpg},{id:19860,name:Karl Yune,character:Tak Mashido,order:8,cast_id:18,profile_path:/qK315vPObCNdywdRN66971FtFez.jpg},{id:111206,name:Olga Fonda,character:Farra Lemkova,order:9,cast_id:19,profile_path:/j1qabOHf3Pf82f1lFpUmdF5XvSp.jpg},{id:53176,name:John Gatins,character:Kingpin,order:10,cast_id:41,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:1126350,name:Sophie Levy,character:Big Sister,order:11,cast_id:42,profile_path:null},{id:1126351,name:Tess Levy,character:Little Sister,order:12,cast_id:43,profile_path:null},{id:1126352,name:Charlie Levy,character:Littlest Sister,order:13,cast_id:44,profile_path:null},{id:187983,name:Gregory Sims,character:Bill Panner,order:14,cast_id:45,profile_path:null}],crew:[{id:58726,name:Leslie Bohem,department:Writing,job:Screenplay,profile_path:null},{id:53176,name:John Gatins,department:Writing,job:Screenplay,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:17825,name:Shawn Levy
You're searching from the first occurrence of "name:" until the "department:Directing" with everything in between. Instead, you need to restrict what can be between the two strings. Here for example I'm saying that the characters that make up the name can only be alphanumeric or a space: string.match(fixstrdirector,"name:([%w ]+),department:Directing") Alternatively, given that there's a comma separating the parameters, a better approach would be to search for "name:" followed by any characters other than a comma, followed by "department:Directing": string.match(fixstrdirector,"name:([^,]+),department:Directing") Of course that wouldn't work if the name had a comma it in!
Lua patterns provides - modifier for tasks as you have above. As stated on PiL - Section 20.2: The + modifier matches one or more characters of the original class. It will always get the longest sequence that matches the pattern. Like *, the modifier - also matches zero or more occurrences of characters of the original class. However, instead of matching the longest sequence, it matches the shortest one. Next, when you are using . to match, it'll find any and all characters satisfying the pattern. Therefore, you'll get the result from first occurence of name until the ,department:Directing is found. Since you know that it is a JSON data, you can try to match for [^,]; that is, non-comma characters. So, for your case try: local tAllNames = {} for sName in fixstrdirector:gmatch( "name:([^,]-),department:Directing" ) do tAllNames[ #tAllNames + 1 ] = sName end and all your required names will be stored in the table tAllNames. An example of the above can be seen at codepad.