특정 글자 사이에 있는 글자 추출 방법은 뭐가있을까요

locdate 20230122 locdate

이중에서 사이에 있는 날짜만 추출 하고 싶습니다.
그런데 제가 아는 상식선에 코드를 입력해도 안되서요 ㅠㅠㅠ
어떤 코드를 써야 할까요?

Hi @amucotomolrayo

Say string is stored in str then

str.split("loc date",stringsplitoptions.none)(1)

Or

str.replace("loc date","").Trim

Or

system.text.regularexpressions.regex.matches(str,"\d{8}")(0).Value

These are to be used in assign with a string variable on left side

Str is the input string

Either of this can be used

Cheers

Thank you for your answer.

But there’s one problem.

resultstr.replace(“locdate”,“”).Trim If you write this sentence,

( locdate ) 20230122 (/locdate ) amongst locdate Only this will be extracted

20230122 20230301 I’d like to extract the date between locates.

Is there any way? I earnestly ask for your help.

Hi @amucotomolrayo

Please use this

In Assign activity(left variable type is matchcollection)

Matchcol = System.text.RegularExpressions.Regex.Matches(str,"(?<=\<locdate\>).*(?=\<\/locdate\>)")

To access the matchcol

  1. Use for loop and ‘in’ argument will be ‘matchcol’ and change for loop type argument to ‘system.text.regularexpressions.regex.match’
  2. Inside the loop use log message with currentitem.value

Cheers

Cheers

문자열에서 숫자만 추출하는건 어떨까요?

System.Text.RegularExpressions.Regex.Replace(yourStr, “[^0-9]+”,String.Empty)

내용을 보니까 공공API에서 받은 XML 데이터에서 특정 요소만 추출하는 것을 하시는 것 같네요.
Regex 이용한 방법은 다들 잘 아시는 것 같아서 다른 방법 한번 적어봅니다.

UiPath.WebAPI.Activites 패키지(대부분 패키지 관리에서 추가해주어야 함) 내의 Deserialize XML 액티비티를 통해서 추출이 가능합니다.
이 액티비티는 XML형태의 String을 입력으로 주면 XDocument 변수로 반환해줍니다.

XDocument 변수에서 하위 요소를 탐색하는데는 아래 함수가 사용가능합니다.
.Element(이름) : 이름에 해당하는 첫 번째 자식 엘리먼트만
.Elements(이름): 이름에 해당하는 모든 자식 엘리먼트들 (배열처럼 For each 가능)
.Value : 해당 엘리먼트에서 태그명을 제외한 순수한 문자열 값

따라서 위와 같은 데이터에서 날짜만 추출하고 싶으신 경우 아래와 같은 방법으로도 추출이 가능합니다.
(*s 유무 주의)

  1. XDocument변수명.Element(“response”).Element(“body”).Element(“items”).Elements(“item”)을 For each에 사용

  • TypeArgument 속성은 System.Xml.Linq.XElement로 설정합니다.
  1. for each 내에서 각 item마다 item.Element(“locdate”).Value를 Assign

  • 딕셔너리 타입 등의 변수를 활용하면 위 그림 처럼 연관된 값을 매핑하듯이 저장할 수도 있습니다. 위 그림은 공휴일 이름에 해당하는 공휴일 날짜를 저장하는 예시 입니다.

그냥 이런 방법도 있다 정도로 참고하시면 좋을 것 같습니다.