4
$\begingroup$

I have a list of points as strings in the form

{"A1=(345.2345,3423.1)","B=(2123,97.123)","KX=(2144.546,-4455)"} 

and I would like to extract the names using RegularExpression. For a single point I tried

StringCases["A1=(345.2345,3423.1)", RegularExpression["^.+\\="]]

but the output is

{"A1="}

How can I extract only the name (everything that is on the left of "=" at the beginning of the string) without the character "=" itself?

$\endgroup$
2
  • 1
    $\begingroup$ A slight variant of the solution already given: StringCases[lst, RegularExpression["(^.+)[=]"]:>"$1"] where 'lst=your_list $\endgroup$
    – user1066
    Mar 18, 2020 at 9:43
  • 2
    $\begingroup$ In addition, maybe consider StringSplit[lst, "="][[All,1]] or StringSplit[lst, RegularExpression["="]][[All,1]] $\endgroup$
    – user1066
    Mar 18, 2020 at 11:05

3 Answers 3

5
$\begingroup$
ClearAll[f]
f = StringCases[RegularExpression["(^.+)="] :> "$1"] 

f /@ {"A1=(345.2345,3423.1)",  "B=(2123,97.123)", "KX=(2144.546,-4455)"}

{{"A1"}, {"B"}, {"KX"}}

$\endgroup$
4
$\begingroup$

Use positive lookahead:

ClearAll[f]
f = StringCases[RegularExpression["^[^=]+(?==)"]];

f /@ {"A1=(345.2345,3423.1)", "B=(2123,97.123)", "KX=(2144.546,-4455)"}
{{"A1"}, {"B"}, {"KX"}}
$\endgroup$
4
$\begingroup$

Another way, without using regular expressions

StringSplit[#, "="] & /* First /@ {"A1=(345.2345,3423.1)", "B=(2123,97.123)", "KX=(2144.546,-4455)"}

{* {"A1", "B", "KX"} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.