Extracting Specific Strings From Text

SLDKFJL LDSKFJLDSKJFLK

I am trying to extract specific strings from my text. I have a string similar to this:

"blablabla(/)Hello Bob(|)bla(/)Hi(|)blablaba"

and I am trying to concatenate a string array of text between (/) and (|) but I cannot figure out a efficient way to do this. In this example I would want to return "Hello Bob" and "Hi". How can I effectively extract specific strings from a string in Swift?

Antonio

Using a functional approach, you can:

  • split the string by (/), which returns an array of strings
  • for each element of the array, transform it into an array of strings split by (|)
  • filter the resulting array including only values (arrays) having more than one element
  • transform each element (array) of the resulting array into its first element

This is the code:

let array = string.componentsSeparatedByString("(/)")
    .map { $0.componentsSeparatedByString("(|)") }
    .filter { $0.count > 1 }
    .map { $0[0] }

Given this input string:

let string = "blablabla(/)Hello Bob(|)bla(/)Hi(|)blablaba"

the result is:

["Hello Bob", "Hi"]

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

extracting multiple strings from a line

분류에서Dev

Extracting Text From HTML With SQL

분류에서Dev

Python: Reading a webpage and extracting text from that page

분류에서Dev

extracting a subset of lines from a large text file

분류에서Dev

Extracting columns from a text file with no delimiters

분류에서Dev

Removing Specific Strings from File

분류에서Dev

Replace specific strings in text with clickable links

분류에서Dev

Extracting a specific element from each cell within cell array

분류에서Dev

how to capture specific strings from line

분류에서Dev

Extracting text after "?"

분류에서Dev

set android:text from strings.xml

분류에서Dev

Extracting Formula from String

분류에서Dev

extracting power from polynomials

분류에서Dev

extracting information from file

분류에서Dev

R Extract specific text from variable

분류에서Dev

Extract a specific text pattern from a string

분류에서Dev

Extract specific records from a text file

분류에서Dev

Python to extract specific numbers from text file

분류에서Dev

read specific values from text file

분류에서Dev

Conduit and Attoparsec - extracting delimited text

분류에서Dev

Extracting values from a text file having | pipe as a delimiter in text file using awk command and Replacing new lines with <br> tag using sed?

분류에서Dev

Extract values from text strings using Matlab's sscanf

분류에서Dev

Extracting specific data in a file using regex

분류에서Dev

Pandas - Extracting data from Series

분류에서Dev

Extracting integers from a query string

분류에서Dev

extracting ip address from a file

분류에서Dev

Extracting modulus from kSecAttrApplicationTag in iOS

분류에서Dev

Extracting a table from webpage with regex

분류에서Dev

Extracting value from nested dictionary

Related 관련 기사

뜨겁다태그

보관