Extract First Word from a Column and Insert into New Column

Extract first word from a column and insert into new column

You can use a regex ("([A-Za-z]+)" or "([[:alpha:]]+)"or "(\\w+)") to grab the first word

Dataframe1$COL2 <- gsub("([A-Za-z]+).*", "\\1", Dataframe1$COL1)

obtaining first word in the string

A very simple approach with gsub

gsub("/.*", '', y)
[1] "london" "newyork" "paris"

Extract first and last words from strings as a new column in pandas

Let's try str.extract here:

df['Profile'].str.extract(r'^(?P<First>\S+).*?(?P<Last>\S+)?$')

First Last
0 Technician NaN
1 Service Engineer
2 Sales Engineer

Not many str methods will be as elegant as this because of the additional need to handle sentences with one word only.


You can also use str.partition here.

u = df['Profile'].str.partition()
pd.DataFrame({'First': u[0], 'Last': u[2].str.split().str[-1]})

First Last
0 Technician NaN
1 Service Engineer
2 Sales Engineer

Extract words from observations in one column to new columns in R

Use tidyr::separate:

library(tidyverse)
DF %>%
separate(Category, into = c("gender", "new.column"), remove = FALSE, extra = "merge")

How to extract first and remaining words in postgres

Convert the values to an array and use that:

select (string_to_array(t, ' '))[1] as first_word,
(string_to_array(t, ' '))[2:99]
from test;

Arrays are easier to work with than strings, particularly if you have lists within a row. However, you can convert back to a string if you like using array_to_string().

In your version of Postgres, this is more accurately written as:

select (string_to_array(t, ' '))[1] as first_word,
(string_to_array(t, ' '))[2:cardinality( string_to_array(t, ' ') )]
from test;

In Postgres 9.6, this shorthand is supported:

select (string_to_array(t, ' '))[1] as first_word,
(string_to_array(t, ' '))[2:]
from test;

Extract the first word OR the first and second words from a range of cells

I guess you're after this code (explanations in comments)

Option Explicit

Sub ExtractMissingInfo2()
Dim MFG As Variant
Dim cell As Range

With Workbooks("trial1").Worksheets("Final Data") 'reference your wanted workbook and worksheet
For Each cell In .Range("D2", .Cells(Rows.Count, 4).End(xlUp)).SpecialCells(xlCellTypeConstants) 'loop thorugh referenced sheet column D not empty cells from row 2 down to last not empty row

MFG = Split(cell.Text, " ") ' separate each word

If UBound(MFG) > 1 Then ReDim Preserve MFG(0 To UBound(MFG) - 2) ' if there were more than two words, keep all but the last two ones

cell.Offset(, 12).Value = Join(MFG, " ") ' write remaining words into column P same row of current cell
Next
End With
End Sub

MySQL only selecting first word of field

Your code

<input type="text" name="address1" <?php echo 'value=' . $address1; ?> required />

will generate an incorrect html tag as:

<input type="text" name="address" value=123 First Street  required />

Try change it to:

<input type="text" name="address" value="<?php echo $address; ?>" required />

The best practice to avoid such mistake is to write the complete html template/tag first before inserting the php code.

Updated

As pointed out by @user2864740, it is better to convert the special character that occurred in an input string into the form of HTML character entities.

<input type="text" name="address" value="<?php htmlentities($address1); ?>" required />


Related Topics



Leave a reply



Submit